Doctrine OrphanRemoving=true删除所有相关实体

Doctrine OrphanRemoving=true删除所有相关实体,doctrine,api-platform.com,symfony3.x,Doctrine,Api Platform.com,Symfony3.x,我对两个实体之间的关系有一个奇怪的问题: 一个Joboffer可以有多个JobOfferLocation,而多个JobOfferLocation只能有一个Joboffer: class Joboffer { /** * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * @ORM\Column(name="id_joboffer", type="integer", length=255, nullable=fal

我对两个实体之间的关系有一个奇怪的问题: 一个Joboffer可以有多个JobOfferLocation,而多个JobOfferLocation只能有一个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);
    }
jobofferlocationclass:

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;
    }
在更新时,我遇到了以下问题: 当我使用“orphanRemoving=true”时,它会删除所有jobofferlocation实体;当我不使用它时,但使用“cascade=remove”时,它不会删除不在关系中的实体。 那么,有没有办法更新所有关系?(删除不再需要的,添加新的和更新现有的。)

我找到了一个答案:

首先,需要addJObofferLocation和removeJobofferLocation方法,并且必须将OrphanRemoving设置为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();
}