好的...我觉得这个问题很愚蠢但是......
我有一个包含以下实体的实体:
供应商:
/**
* @ORM\OneToMany(targetEntity="SRC\Bundle\MarketingBundle\Entity\Ad", mappedBy="vendor", cascade={"all"})
*
* @var Collection|Ad[]
*/
protected $ads;
/**
* Returns all vendor ads.
*
* @return Collection|Ad[]
*/
public function getAds()
{
return $this->ads;
}
/**
* Sets all vendor ads.
*
* @param Collection $ads
*/
public function setAds(Collection $ads)
{
die("SET ADS. DIE! DIE!");
$this->ads = $ads;
return $this;
}
/**
* Adds vendor ad.
*
* @param Ad $ad
*/
public function addAd(Ad $ad)
{
if (!$this->hasAd($ad)) {
$ad->setVendor($this);
$this->ads->add($ad);
}
return $this;
}
/**
* Removes vendor ad.
*
* @param Ad $ad
*/
public function removeAd(Ad $ad)
{
if ($this->hasAd($ad)) {
$this->ads->removeElement($ad);
$ad->setVendor(null);
}
return $this;
}
/**
* Checks whether vendor has given ad.
*
* @param Ad $ad
*
* @return Boolean
*/
public function hasAd(Ad $ad)
{
return $this->ads->contains($ad);
}
广告:
/**
* @ORM\ManyToOne(targetEntity="SRC\Bundle\VendorBundle\Entity\Vendor", inversedBy="products", fetch="LAZY")
* @ORM\JoinColumn(name="vendor_id", referencedColumnName="id", nullable=true)
* @var Vendor
*/
protected $vendor;
/**
* Get vendor.
*
* @return Vendor
*/
public function getVendor(){
return $this->vendor;
}
/**
* Set vendor.
*
* @param Vendor $vendor
*/
public function setVendor(Vendor $vendor){
$this->vendor = $vendor;
return $this;
}
当我更新广告" setAds()"时,供应商CRUD中存在问题。永远不会被调用,因为你可以在那里看到die(),它永远不会被执行。我做错了什么???
答案 0 :(得分:0)
首选“添加”方法到“设置”
public function addAd(\SRC\Bundle\MarketingBundle\Entity\Ad $ad)
{
if(!$this->ads->contains($ad)) {
$this->ads[] = $ad;
}
return $this;
}
public function removeAd(\SRC\Bundle\MarketingBundle\Entity\Ad $ad)
{
$this->ads->removeElement($ad);
}
public function getAds()
{
return $this->ads;
}
我假设您可能在Ad类中调用 setVendor 方法来执行关系,同步双方执行以下操作:
public function setVendor(Vendor $vendor){
$this->vendor = $vendor;
$vendor->addAd($this);
return $this;
}
您还应该在Vendor构造函数中初始化ArrayCollection:
public __construct()
{
$this->ads = new \Doctrine\Common\Collections\ArrayCollection();
}