What is the best practice for creating custom properties / functions for Doctrine Objects/Entites ?
For example in Symfony1.4 / Doctrine 1.2 I could add a method to the User class
public function getName()
{
return $this->getFirstName().' '.$this->getLastName();
}
This would combine to fields and return the value – a custom property / function.
Now where the heck do i do this in Symfony2 / Doctrine 2 – the entity class ? is it OK to put custom methods in there ? I am using the EntityRepositories for custom methods to find data … but not sure on the best way to do it for an object.
I have searched high and low and cannot find anything related to this in the documentation (both Symfony and Doctrine) …. please put me out of my misery !
A Doctrine entity is really just a plain old PHP object, with additional mapping information on how to persist some or all of the properties of the object to the database. You can add anything you want to the class, and not all properties or methods have to map to a field in the database.
For your specific concern, your
getName()method is just fine as far as best practice goes. Note that to update the fields, you’d either have to use thesetFirstName()andsetLastName()methods, or perhaps write asetName()method that explodes on a space and updates the individual fields.