我有很多这种类型的关系,但我不明白为什么这种关系不起作用。
我有一个委托和一个促销实体:
委托
晋升
/**
* Company\CBundle\Entity\Promotion
*
* @ORM\Entity
* @DoctrineAssert\UniqueEntity("promotionCode")
*/
class Promotion
{
const REGISTER_DAYS = 30;
const REGISTER_DISCOUNT = 100;
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="promotionCode", type="string", unique=true, nullable=true)
*/
protected $promotionCode;
/**
* @ORM\Column(name="name", type="string")
*/
protected $name;
/**
* @ORM\Column(name="description", type="string", nullable=true)
*/
protected $description;
/**
* @ORM\Column(name="days", type="integer")
*/
protected $days;
/**
* @ORM\Column(name="discount", type="float")
*/
protected $discount;
/**
* @ORM\ManyToOne(targetEntity="Delegation", inversedBy="promotions")
* @ORM\JoinColumn(name="delegation_id", referencedColumnName="id")
*/
private $delegation;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="promotions")
*/
private $product;
/**
* @var date $adquiredDate
*
* @ORM\Column(name="adquiredDate", type="date", nullable=true)
*/
private $adquiredDate;
当我在控制器中创建促销时,表 Promotion 具有与委托相关的新对象
private function createPromotion($delegation)
{
$em = $this->getDoctrine()->getEntityManager();
$promotion = Promotion::createPromotion($delegacion, Promotion::REGISTER_DAYS, Promotion::REGISTER_DISCOUNT);
$em->persist($promotion);
$em->persist($delegation);
$em->flush();
}
数据库
*************************** 15. row ***************************
id: 32
delegation_id: 19
days: 20
discount: 50
adquiredDate: 2013-01-10
*************************** 16. row ***************************
id: 33
delegation_id: 19
days: 25
discount: 50
adquiredDate: 2013-01-10
*************************** 17. row ***************************
id: 34
delegation_id: 19
days: 30
discount: 50
adquiredDate: 2013-01-10
但是当我在另一个控制器/动作中调用 $delegation->getPromotions() 时,没有促销,返回一个没有数据的 Doctrine\ORM\PersistentCollection。
有人可以帮忙吗?
编辑更多信息。$delegation->getPromotions() 是空的,但是寻找该委托的提升并调用 $promotion->getDelegation() 正在正确地返回委托:?