【问题标题】:ManyToMany relation, orphanRemoval多对多关系,orphanRemoval
【发布时间】:2017-10-31 14:11:43
【问题描述】:

我有 2 个实体:

  • 位点
  • 参考

在每个 Locus 上,我们可以定义许多不同的 References,并且一个 Reference 可以被 Many Locus 使用。然后我有一个多对多关系。

但是,在我的情况下,如果没有通过 Locus 链接,Reference 什么都不是,我不想将它保留在我的 BDD 中。

尝试列表:

  • Reference 拥有的关系:
    • ReferenceEntity 中的orphanRemoval(在拥有方):删除轨迹
    • Lo​​cusEntity 中的orphanRemoval(在 InversedSide 上):什么都不做
  • Lo​​cus 拥有的关系:
    • ReferenceEntity 中的 orphanRemoval(在 InversedSide 上):什么都不做
    • 在 LocusEntity 中的orphanRemoval(在拥有方):删除引用,尽管其他 Locus 使用了

参考.php

/**
 * Reference
 *
 * @ORM\Table(name="reference")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ReferenceRepository")
 */
class Reference
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Locus", inversedBy="references")
     */
    private $locus;

    public function addLocus(Locus $locus)
    {
        if (!$this->locus->contains($locus)) {
            $this->locus->add($locus);
            $locus->addReference($this);
        }

        return $this;
    }

    public function removeLocus(Locus $locus)
    {
        if ($this->locus->contains($locus)) {
            $this->locus->removeElement($locus);
        }

        return $this;
    }

    public function getLocus()
    {
        return $this->locus;
    }

Locus.php

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\LocusRepository")
 */
class Locus extends GeneticEntry
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Reference", mappedBy="locus", orphanRemoval=true)
     */
    private $references;

    public function addReference(Reference $reference)
    {
        $this->references->add($reference);

        return $this;
    }

    public function removeReference(Reference $reference)
    {
        if ($this->references->contains($reference)) {
            $this->references->removeElement($reference);
        }

        return $this;
    }

    public function getReferences()
    {
        return $this->references;
    }

【问题讨论】:

  • 您可以尝试在LocusDeleted上创建一个事件,以便在您内部检查其相关引用是否包含不相关的其他引用,如果存在则将其删除
  • 是的,这是真的,我没有想过这样做,我不知道为什么,但我只是花了很长时间搜索并尝试使用原生 Doctrine 方式来做到这一点。
  • 我也有同样的问题。有没有人同时找到 Doctrine 解决方案?

标签: symfony doctrine-orm


【解决方案1】:

目前我选择在 postUpdate 事件上使用 EventListener,我测试 ArrayCollection,如果为空,我删除实体:

ReferenceListener.php:

<?php

namespace AppBundle\EventListener;

use AppBundle\Entity\Reference;
use Doctrine\ORM\Event\LifecycleEventArgs;

class ReferenceListener
{
    public function postUpdate(LifecycleEventArgs $args)
    {
        $object = $args->getObject();
        if (!$object instanceof Reference) {
            return;
        }

        // If the Reference have no Locus and no Chromosomes, delete it
        if ($object->getLocus()->isEmpty() && $object->getChromosomes()->isEmpty()) {
            $args->getEntityManager()->remove($object);
            $args->getEntityManager()->flush();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-03-13
    • 2018-09-09
    • 2018-06-15
    • 2014-10-11
    相关资源
    最近更新 更多