简体   繁体   中英

doctrine 2 get related entities in PreFlush

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) ?

Hope my question is clear enough..

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.

Happy Christmas :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM