My general question is how to create entities and repositories with symfony2?
Well, entity is a type of object that is used to hold data. Each instance of entity holds exactly one row of targeted database table. As for the directories, Symfony2 has some expectations where to find classes - that goes for entities as well.
With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.
The entity is the admin, and the entity class is the class that represents admins. In the same way that the admin entity inherits a number of properties from the user entity, the Admin entity class could extend a User entity class.
A repository in a term used by many ORMs (Object Relational Mappers), doctrine is just one of these. It means the place where our data can be accessed from, a repository of data. This is to distinguish it from a database as a repository does not care how its data is stored.
My solutions (i don't know if it's good, best practice!?)
YML
Create "Entities.User.dcm.yml" file in HelloBundle/Resources/config/doctrine/metadata/orm with this code (by example):
Entities\User:
type: entity
table: users
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 50
Then
php app/console doctrine:mapping:import "HelloBundle" yml
php app/console doctrine:generate:entities "HelloBundle"
Then, you can test it in your controller with:
$user = new \Sensio\HelloBundle\Entity\User;
$user->setName('Acubens');
$em = $this->get('doctrine.orm.entity_manager');
$em->persist($user);
$em->flush();
or with PHP
Create "User.php" file in HelloBundle\Entity with this code
// Sensio/HelloBundle/Entity/User.php
namespace Sensio\HelloBundle\Entity;
/**
* @orm:Entity
*/
class User
{
/**
* @orm:Id
* @orm:Column(type="integer")
* @orm:GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @orm:Column(type="string", length="255")
*/
protected $name;
}
Then
php app/console doctrine:mapping:import "HelloBundle"
this will generate in "HelloBundle/Resources/config/doctrine/metadata/orm"
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Sensio\HelloBundle\Entity\User" table="user">
<change-tracking-policy>DEFERRED_IMPLICIT</change-tracking-policy>
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="name" type="string" column="name" length="255"/>
<lifecycle-callbacks/>
</entity>
</doctrine-mapping>
Then delete User.php
php app/console doctrine:generate:entities "HelloBundle"
After you have a new nice file User.php
<?php
namespace Sensio\HelloBundle\Entity;
/**
* Sensio\HelloBundle\Entity\User
*/
class User
{
/**
* @var string $name
*/
private $name;
/**
* @var integer $id
*/
private $id;
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string $name
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}
}
And you how do you do? Why i must specify HelloBundle in my commands?
ps: my config.yml
doctrine.dbal:
driver: pdo_mysql
dbname: sf2
user: root
password:
logging: %kernel.debug%
doctrine.orm:
auto_generate_proxy_classes: %kernel.debug%
mappings:
HelloBundle: ~
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With