這是我的實體 「產品」:的Symfony:一對多Bidirectionnal關係
class Product
{
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/*
* @ORM\OneToMany(targetEntity="ShopBundle\Entity\Offer", mappedBy="product", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
*/
private $offers;
/*
* Constructeur
*/
public function __construct()
{
$this->offers = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add offer
* @param \ShopBundle\Entity\Offer $offer
* @return Product
*/
public function addOffer(\ShopBundle\Entity\Offer $offer)
{
$this->offers[] = $offer;
return $this;
}
/**
* Remove offer
* @param \ShopBundle\Entity\Offer $offer
*/
public function removeOffer(\ShopBundle\Entity\Offer $offer)
{
$this->offers->removeElement($offer);
}
/**
* Get parameters
* @return \Doctrine\Common\Collections\Collection
*/
public function getOffers()
{
return $this->offers;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
而且,這是我的實體 「要約」:
class Offer
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="ShopBundle\Entity\Product", inversedBy="offers", cascade={"persist", "merge"})
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
*/
private $product;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set product
*
* @param \ShopBundle\Entity\Product $product
*
* @return Offer
*/
public function setProduct(\ShopBundle\Entity\Product $product)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return \ShopBundle\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
}
時候,我得到了我的控制器中的物體 $ product = $ em-> getRepository('ShopBundle:Product') - > find($ id);
然後,$ product-> getOffers()返回null,但是在數據庫中不存在null。
幫助我......請......
什麼結果返回$ EM-> getRepository( 'ShopBundle:優惠') - > findByProduct($產品)? –
它會返回「產品」 – Gazouweb
的好元素「offer」,您是否嘗試清除Symfony緩存?或做緩存熱身?也可以運行'app/console doctrine:mapping:info'來查看映射是否正確,並且可能清除了教條緩存'app/console doctrine:cache:clear-metadata', 'app/console doctrine:cache:clear-query', 'app/console doctrine:cache:clear-result' – takeit