There are following entities: Zone
, ZoneRecord
ZoneRecord
has a Method validate()
to validate against all other ZoneRecord
's of related Zone
.
Now I want to check / validate each ZoneRecord
(the ones which are saved already plus the ones which are added by Zone->addRecord(ZoneRecord)
on runtime) which is related to the Zone
if Zone
gets saved.
Right now I have a PreFlush
Lifecyclecallback ZoneRecord->validate
where I trigger this->getZone->getRecords()
: this methods gives me only the already saved entities which are in db.
How can I get ALL related Entities of Zone
(the saved from DB and the dynamicly added)?
The Problem seems to be in Doctrine internal. I use InheritanceType "JOINED".
The annotation in Zone
looks like:
/**
* @var ZoneRecord[]
* @ORM\OneToMany(targetEntity="Application\Entity\ZoneRecord", mappedBy="zone", cascade={"all"}, orphanRemoval=true)
*/
protected $records;
/**
* @var ZoneRecordA[]
* @ORM\OneToMany(targetEntity="Application\Entity\ZoneRecordA", mappedBy="zone", cascade={"all"}, orphanRemoval=true)
*/
protected $recordsa;
The annotation from ZoneRecord
:
/**
* @ORM\Entity
* @ORM\Table(name="zonerecords")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({
* "A" = "Application\Entity\ZoneRecordA"
* })
* @ORM\HasLifecycleCallbacks
*/
abstract class ZoneRecord
The annotation from ZoneRecordA
:
/**
*
* @ORM\Entity
* @ORM\Table(name="zonerecords_a")
*
*/
class ZoneRecordA extends ZoneRecord
If I add or remove a ZoneRecord
from Zone
via Zone->addRecordA(ZoneRecord)
/ Zone->removeRecordA(ZoneRecord)
it will only be removed from the protected $recordsa;
stack. The protected $records;
will be untouched until the entity is successfully flushed and reloaded.
It seems that Doctrine does internal differ between ZoneRecord
and ZoneRecordA
although they are marked as JOINED and ZoneRecordA
extends ZoneRecord
.
This is especially a problem if you want to delete Records - right now I use both delete Methods (Zone->removeRecord(ZoneRecordA)
and Zone->removeRecordA(ZoneRecordA)
) to be sure the Entity is removed from the EntityManager.
This strange behaviour also occurs if you add entitys to Zone
via addRecord()
or addRecordA()
, they are used as different collections until they are flushed and reloaded.