orphanRemoval=true 移除所有相关实体
orphanRemoval=true removes all related entities
我对两个实体之间的关系有一个奇怪的问题:
一个 Joboffer 可以有多个 jobofferLocations,而 Many 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);
}
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;
}
关于更新,我遇到了这个问题:
当我使用 "orphanRemoval=true," 时,它会删除所有 jobofferlocation 实体,当我不使用它时,但 "cascade=remove",它不会再删除那些不在关系中的实体。
那么,有没有办法更新所有的关系呢? (删除不再需要的,添加新的并更新现有的。)
我找到了答案:
首先,需要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();
}
我对两个实体之间的关系有一个奇怪的问题: 一个 Joboffer 可以有多个 jobofferLocations,而 Many 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);
}
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;
}
关于更新,我遇到了这个问题: 当我使用 "orphanRemoval=true," 时,它会删除所有 jobofferlocation 实体,当我不使用它时,但 "cascade=remove",它不会再删除那些不在关系中的实体。 那么,有没有办法更新所有的关系呢? (删除不再需要的,添加新的并更新现有的。)
我找到了答案:
首先,需要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();
}