DoctrineTraitBundle
Generate the entities stub methods in a trait, (*1)
The generated traits contains the getters/setters generated by the doctrine generator.
The trait should be used in your entity., (*2)
Benefits
Your entities contains only the usefull information:, (*3)
- The doctrine columns and relationships
- The getters/setters that have been modified
- The custom methods
All basics getters/setters are in the trait., (*4)
Composer
Use composer to get the bundle, (*5)
composer require --dev "a5sys/doctrine-trait-bundle"
Activate the bundle
In the AppKernel, activate the bundle for the dev environment, (*6)
if (in_array($this->getEnvironment(), array('dev'))) {
...
$bundles[] = new A5sys\DoctrineTraitBundle\DoctrineTraitBundle();
}
Usage
Generate traits
Run the command, (*7)
php app/console generate:doctrine:traits AppBundle\\Entity
Or this one if you want to act on a single entity, (*8)
php app/console generate:doctrine:traits "AppBundle:MyEntity"
Those commands will generate the trait(s) in the /src/AppBundle/Entity/Traits directory, (*9)
Use custom method
- Go in the trait related to your entity
- Cut the method
- Go in your entity
- Paste the method
- Update the method
- The command will not re-generate this method because it is already defined in entity
Exemple
- Create your entity (User) without the getters/setters
- Run the command
- Add the trait to your entity
- You are done
The entity
namespace AppBundle/Entity;
class User
{
use AppBundle/Entity/Traits/UserTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer", nullable=false)
*/
protected $id;
/**
* @ORM\Column(type="string", length=50, nullable=false)
*/
protected $name;
}
The trait
namespace AppBundle/Entity/Traits;
/**
* User
*/
trait UserTrait
{
/**
*
* @param integer $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get the name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}