I want to create some Symfony2 bundles which are reusable accross different projects, but where the entities also can be easily extended if required.
An example could be a reusable UserBundle, which contains a User entity with all the ORM mappings defined. In my application however, I might want to extend this entity and add extra columns, associations or override some of the parent's mappings.
The closest solution I could find are Doctrine2's mapped superclasses, but then I'd lose the plug-and-playness of my reusable bundle, I'd always have to extend the mapped superclass in my application even if I don't wish to modify the mappings.
The other documented inheritance schemes require modifying the parent's mappings, and then my UserBundle wouldn't be portable anymore accross projects.
Is there a way to define a fully-working entity in one bundle, and still extend that in another bundle?
For future reference, this can be solved using target entity resolution.
You can find extra information in Symfony docs.
The steps are pretty straighforward:
Create an interface in your bundle for the User
entity
namespace Acme/UserBundle/Model;
interface UserInterface
{
// public functions expected for entity User
}
Make your base User
entity implement the interface
namespace Acme/UserBundle/Entity;
/**
* @ORM\Entity
*/
class User implements UserInterface
{
// implement public functions
}
Create relationships as usual, but using the interface
namespace Acme/InvoiceBundle/Entity;
/**
* @ORM\Entity
*/
class Invoice
{
/**
* @ORM\ManyToOne(targetEntity="Acme\UserBundle\Model\UserInterface")
*/
protected $user;
}
Configure the listener by adding the following to config.yml
doctrine:
# ....
orm:
# ....
resolve_target_entities:
Acme\UserBundle\Model\UserInterface: Acme\UserBundle\Entity\User
If you want to customize User
entity for your current application
Extend from the User
class or implement UserInterface
namespace Acme/WebBundle/Entity;
use Acme/UserBundle/Entity/User as BaseUser;
/**
* @ORM\Entity
*/
class User extends BaseUser
{
// Add new fields and functions
}
Configure the listener accordingly
doctrine:
# ....
orm:
# ....
resolve_target_entities:
Acme\UserBundle\Model\UserInterface: Acme\WebBundle\Entity\User
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