gpt4 book ai didi

symfony - Doctrine2 OneToMany - ManyToOne 返回空集合,数据库正常

转载 作者:行者123 更新时间:2023-12-04 19:11:48 25 4
gpt4 key购买 nike

我有很多这种类型的关系,但我不明白为什么这种关系不起作用。
我有一个代表团和一个促销实体:
代表团

晋升

    /**
* 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;
}

编辑

好的,我为你创建了一个对我有用的简约版本。您可以从那里构建它或观察与您的代码的差异:

促销.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();
}
}

产品.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)

所以引用实际上被保存并且可以读取。呸。祝你好运! :-)

关于symfony - Doctrine2 OneToMany - ManyToOne 返回空集合,数据库正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14261565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com