Generate the entities stub methods in a trait
The generated traits contains the getters/setters generated by the doctrine generator. The trait should be used in your entity.
Your entities contains only the usefull information:
- The doctrine columns and relationships
- The getters/setters that have been modified
- The custom methods
All basics getters/setters are in the trait.
Use composer to get the bundle
composer require --dev "a5sys/doctrine-trait-bundle"
In the AppKernel, activate the bundle for the dev environment
if (in_array($this->getEnvironment(), array('dev'))) {
...
$bundles[] = new A5sys\DoctrineTraitBundle\DoctrineTraitBundle();
}
Run the command
php app/console generate:doctrine:traits AppBundle\\Entity
Or this one if you want to act on a single entity
php app/console generate:doctrine:traits "AppBundle:MyEntity"
Those commands will generate the trait(s) in the /src/AppBundle/Entity/Traits directory
- 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
- Create your entity (User) without the getters/setters
- Run the command
- Add the trait to your entity
- You are done
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;
}
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;
}
}