I have some concepts with common relationships and attributes. So, I want to simplify my scheme using inheritance matching.
I created the base class BaseData mappedsuperclass and make my other objects extend it. This BaseData class has a common relationship that I need in every entity.
He works with many relationships, such as
class BaseData { protected $service;
But it gets a little more complicated with self-regulation.
For example, since I want to create a parent link, I tried the following:
class BaseData { protected $parent;
Obviously, this throws a TableNotFoundException when I try to query this object: QLSTATE[42S02]: Base table or view not found: 1146 Table 'project.base_data' doesn't exist .
So, I tried AssociationOverrides, but it doesn't seem to allow changing the target entity.
So, is there a way to create some self-esteem in MappedSuperclass? And by the way, does that even make sense?
Thank you very much in advance!
Update
Here is anwser:
I defined protected $parent and protected $children in my BaseData mappedSuperClass, as planned. I added them with other information that I need. eg:
class BaseData { protected $parent; protected $children;
Then I add the ORM relation to the loadClassMetadata event.
/** * @param LoadClassMetadataEventArgs $eventArgs */ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) { // the $metadata is all the mapping info for this class $classMetadata = $eventArgs->getClassMetadata(); $reflObj = new \ReflectionClass($classMetadata->name); if($reflObj) { if ($reflObj->isSubclassOf('CoreBundle\Entity\BaseData')) { $fieldMapping = array( 'targetEntity' => $classMetadata->name, 'fieldName' => 'parent', 'inversedBy' => 'children', 'JoinColumn' => array( 'name' => 'parent_id', 'referencedColumnName' => 'id', 'nullable' => true, 'onDelete' => 'SET NULL', ), ); $classMetadata->mapManyToOne($fieldMapping); $fieldMapping = array( 'fieldName' => 'children', 'targetEntity' => $classMetadata->name, 'mappedBy' => 'parent', ); $classMetadata->mapOneToMany($fieldMapping); } } }
Register an event and it.
Now every class that extends SuperClass BaseData gets a relation. For example, php app/console doctrine:generate:entities MyBundle generates the following code inside a SubClass entity:
public function setParent(\MyBundle\Entity\Subclass $parent = null) { $this->parent = $parent; return $this; } public function getParent() { return $this->parent; } public function addChild(\MyBundle\Entity\Subclass $child) { $this->children[] = $child; return $this; } public function removeChild(\MyBundle\Entity\Subclass $child) { $this->children->removeElement($child); } public function getChildren() { return $this->children; }