我有很多这种类型的关系,但是我不明白为什么这种关系不起作用。
我有一个代表团和一个促进实体:
代表团
晋升
/**
* 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;
在 Controller 中创建促销时,表Promotion具有与委托(delegate)相关的新对象
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
但是,当我在另一个 Controller / Action 中调用$ delegation-> getPromotions()时,没有提升,则返回没有数据的Doctrine\ORM\PersistentCollection。
有人可以帮忙吗?
编辑更多信息。
$ delegation-> getPromotions()为空,但是正在寻找对该委托(delegate)的升级并调用$ promotion-> getDelegation()可以正确返回委托(delegate):?
最佳答案
您是否尝试过定义$ delegation属性,例如
/**
* @ORM\ManyToOne(targetEntity="Delegation", inversedBy="promotions")
* @ORM\JoinColumn(name="delegation_id", referencedColumnName="id")
*/
private $delegation;
参见Doctrine2 Docs: Association Mapping->Many-To-One
您的代码中也有很多错别字。例如
/**
* @ORM\OneToMany(targetEntity="Promotion", mappedBy="delegacion", cascade={"all"}, orphanRemoval=true)
*/
protected $promotions;
mappedBy="delegacion"
应该是mappedBy="delegation"
。或者
public function getDeleTacion()
{
return $this->deleTacion;
}
应该
public function getDelegation()
{
return $this->delegation;
}
编辑
好的,我为您创建了一个对我有用的简约版本。您可以从那里构建它,或注意代码之间的差异:
Promotion.php
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Promotion
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Delegation", inversedBy="promotions", cascade={"persist"})
* @ORM\JoinColumn(name="delegation_id", referencedColumnName="id")
*/
public $delegation;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="promotions", cascade={"persist"})
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
public $product;
}
委托(delegate).php
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Delegation
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Promotion", mappedBy="delegation", cascade={"all"}, orphanRemoval=true)
*/
public $promotions;
public function __construct() {
$this->promotions = new \Doctrine\Common\Collections\ArrayCollection();
}
}
Product.php
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Promotion", mappedBy="product", cascade={"all"}, orphanRemoval=true)
*/
public $promotions;
public function __construct() {
$this->promotions = new \Doctrine\Common\Collections\ArrayCollection();
}
}
如果你现在做类似的事情
$delegation = new Delegation();
$product = new Product();
$promotion = new Promotion();
$promotion->delegation = $delegation;
$promotion->product = $product;
$em->persist($promotion);
$em->flush();
$products = $em->createQuery('select p from BundleName\Entity\Product p')->execute();
$delegations = $em->createQuery('select d from BundleName\Entity\Delegation d')->execute();
var_dump(count($products[0]->promotions), count($delegations[0]->promotions));
你应该以
int(1)
int(1)
因此,实际上已保存了反射并且可以读取。 ew祝你好运! :-)
关于symfony - Doctrine2 OneToMany-ManyToOne返回空集合,数据库正常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14261565/