Reputation: 2331
I a project of mine I need to define two bundles A and B. In project A I have an entity A.E and in project B I have an entity B.E.
I need a bidirectionnal relationship between A.E and B.E. But I also need to be able to replace bundle B by another bundle C that would have an entity C.E. I will then replace the A.E-B.E bidirectional relationship by a A.E-C.E identical relation.
It seems that I cannot define a bidirectional relation ship in doctrine 2 to be reusable:
// pseudo PHP, just to illustrate.
/**
* @manyToOne(targetEntity=B.E)
* @entity
*/
class A.E() {}
/**
* @oneToMany(targetEntity=B.E)
* @entity
*/
class B.E() {}
I can't replace B.E by C.E without changing A.E. I'm used to the python Django ORM where I don't need to declare a relation between two entity in each of them, thus allowing to build reusable entity models.
I can't find a way to build such a reusable model with symfony2. I may have misunderstood something or be going the wrong way. Ay help on that ?
Thanks
Upvotes: 1
Views: 391
Reputation: 9246
You can make your reusable entity to be related to an interface. Basically:
class MyReusableEntity
{
/**
* @ORM\ManyToOne(targetEntity="MyVendor\MyBundle\MyInterface")
* @var MyInterface
*/
protected $myInterfaceRelation;
}
And in each project you use it:
doctrine:
orm:
resolve_target_entities:
MyVendor\MyBundle\MyInterface: My\Concrete\Class
Docs: http://symfony.com/doc/current/cookbook/doctrine/resolve_target_entity.html
Upvotes: 3