orphanRemoval=true removes all related entities

966 Views Asked by At

I have a strange problem with a relation between two entities: One Joboffer can have many jobofferLocations, while Many jobOfferlocations only have one 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);
    }

The 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;
    }

On Updates I have this problem: When I use "orphanRemoval=true,", it removes all jobofferlocation entities, when I don't use it, but "cascade=remove", it doesn't remove the ones that aren't in the relations any more. So, is there a way to update all relations? (Remove the ones that aren't needed any more, adding new ones and updating existing ones.)

1

There are 1 best solutions below

0
sneaky On BEST ANSWER

I found an answer:

first of all, the methods addJObofferLocation and removeJobofferLocation are needed and orphanRemoval must be set to true. The trick seems to be in adding the right (not double) locations.

  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();
}