简体   繁体   English

教义2在PreFlush中获取相关实体

[英]doctrine 2 get related entities in PreFlush

There are following entities: Zone , ZoneRecord 有以下实体: ZoneZoneRecord

ZoneRecord has a Method validate() to validate against all other ZoneRecord 's of related Zone . ZoneRecord有一个方法validate()来对所有相关Zone其他ZoneRecord进行验证。

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. 现在,我要检查/验证每个ZoneRecord (已经保存的文档加上其被添加的那些Zone->addRecord(ZoneRecord)上运行时),这是关系到Zone ,如果Zone被保存。

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 . 现在,我有一个PreFlush Lifecyclecallback ZoneRecord->validate ,在其中触发this->getZone->getRecords()此方法仅给我提供了db中已保存的实体

How can i get ALL related Entities of Zone (the saved from DB and the dynamicly added) ? 如何获取Zone所有相关实体(从数据库保存并动态添加)?

Hope my question is clear enough.. 希望我的问题很清楚。

The Problem seems to be in Doctrine internal. 问题似乎出在教义内部。 I use InheritanceType "JOINED" . 我使用InheritanceType“ JOINED”

The annotation in Zone looks like: Zone的注释如下所示:

/**
 * @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 : 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 : 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; 如果我通过Zone->addRecordA(ZoneRecord) / Zone->removeRecordA(ZoneRecord)Zone添加或删除ZoneRecord ,它将仅从protected $recordsa; stack. 堆。 The protected $records; 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 . 尽管Doctrine被标记为JOINED,并且ZoneRecordA扩展了ZoneRecord ,但Doctrine在ZoneRecordZoneRecordA之间确实在内部有所不同。

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. 如果要删除记录,这尤其是个问题-现在,我要同时使用两种删除方法( Zone->removeRecord(ZoneRecordA)Zone->removeRecordA(ZoneRecordA) )来确保从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. 如果通过addRecord()addRecordA()将实体添加到Zone ,也会将它们用作不同的集合,直到刷新并重新加载它们时,也会发生这种奇怪的行为。

Happy Christmas :) 圣诞快乐 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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