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.)
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.