orphanRemoval = true删除所有相关实体

时间:2018-09-19 16:46:38

标签: doctrine api-platform.com symfony3.x

我对两个实体之间的关系有一个奇怪的问题: 一个Joboffer可以有多个jobofferLocations,而许多jobOfferlocations只能有一个joboffer:

class Joboffer
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id_joboffer", type="integer", length=255, nullable=false)
     * @Groups({"api_read", "api_write"})
     */
    protected $id;
/**

     *
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Joboffer\JobofferLocation", mappedBy="joboffer",  orphanRemoval=true, cascade={"persist"})
     * @Groups({"api_read", "api_write"})
     * @var ArrayCollection
     */
    protected $jobofferLocations;


....
/**
     * @param JobofferLocation $jobofferLocation
     */
    public function addJobofferLocation(JobofferLocation $jobofferLocation)
    {
        if ($this->jobofferLocations->contains($jobofferLocation)) {
            return;
        }
        $this->jobofferLocations->add($jobofferLocation);
        $jobofferLocation->setJoboffer($this);
    }

jobofferlocation类:

class JobofferLocation
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id_joboffer_location", type="integer", length=255, nullable=false)
     * @Groups({"api_read"})
     */
    protected $id;

/**
     * @return mixed
     */
    public function getJoboffer()
    {
        return $this->joboffer;
    }

    /**
     * @param mixed $joboffer
     */
    public function setJoboffer($joboffer)
    {
        $this->joboffer = $joboffer;
    }

在更新中,我有此问题: 当我使用“ orphanRemoval = true”时,它将删除所有jobofferlocation实体;当我不使用它时,它将“ cascade = remove”删除,不再删除那些不在关系中的实体。 那么,有没有办法更新所有关系? (删除不再需要的那些,添加新的并更新现有的。)

1 个答案:

答案 0 :(得分:0)

我找到了答案:

首先,需要方法addJObofferLocation和removeJobofferLocation,并且必须将orphanRemoval设置为true。 诀窍似乎在于添加正确的位置(而不是重复的位置)。

  class Joboffer
  {
     ...       

     /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Joboffer\JobofferLocation", mappedBy="joboffer", orphanRemoval=true,cascade={"persist"})
     * @Groups({"api_read", "api_write"})
     * @var ArrayCollection
     */
    protected $jobofferLocations;
   /**
     * @param JobofferLocation $jobofferLocation
     */
    public function addJobofferLocation(JobofferLocation $jobofferLocation)
    {

    if ($this->jobofferLocations->contains($jobofferLocation)) {
        return;
    }

    /** @var JobofferLocation $location */
    foreach ($this->jobofferLocations as $location){

        //check if this location exists
        // it seems we need this, because of the API plattform bundle
        if ($location->getIdLocation() == $jobofferLocation->getIdLocation()){
            // if it exists, just copy the new jobofferlocation settings

            return;
        }
    }
    $jobofferLocation->setJoboffer($this);
    $this->jobofferLocations->add($jobofferLocation);

}

public function removeJobOfferLocation(JobofferLocation $jobofferLocation)
{
    if (!$this->jobofferLocations->contains($jobofferLocation)) {
        return;
    }
    $this->jobofferLocations->removeElement($jobofferLocation);
    $jobofferLocation->removeJobOffer();
}