I have a problem with the OneToMany <-> ManyToOne bidirectional relationship between my Device and Event objects. This is how the display looks:
// Device entity /** * @ORM\OneToMany(targetEntity="AppBundle\Entity\Event", mappedBy="device") */ protected $events; // Event entity /** * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Device", inversedBy="events") */ protected $device;
The problem arises because Device is a single table inheritance object.
* @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="device_class_type", type="string")
and every time I select and iterate over some Event entities, then $device always willingly selected. This is because it is an STI object, as indicated in the relative documentation.
There is a general performance assessment using a single table Inheritance: if the target from a many-to-one or one-to-one association is an STI object, it is preferable for performance reasons that it is a sheet element in the inheritance hierarchy (i.e. there are no subclasses). Otherwise, Doctrine CANNOT create proxy instances of this and will ALWAYS load the object with impatience.
Now there is another object called Gateway , which has a relationship with both Device and Event :
protected $devices; protected $events; public function getEvents(): Collection { return $this->events; }
And, of course, every time I $gateway->getEvents() over $gateway->getEvents() , all devices with relative events look impatiently. This happens even if I don't get any $device information - an empty foreach enough for Doctrine to execute 1 request for each object to get the associated $device
foreach ($gateway->getEvents() as $event) {}
Now I know that I could use QueryBuilder to set a different hydration mode, avoiding $device fetching
return $this->getEntityManager()->createQueryBuilder() ->select('e') ->from('AppBundle:Event', 'e') ->where('e.gateway = :gateway') ->setParameter('gateway', $gateway) ->getQuery()->getResult(Query::HYDRATE_SIMPLEOBJECT);
but I would like to do it somehow directly in the Gateway object.
So, is it possible to hydrate Gateway->events directly in the Gateway entity class?