Doctrine2 OneToMany - ManyToOne返回空集合,数据库确定

时间:2013-01-10 15:39:28

标签: symfony doctrine-orm one-to-many


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

促销

    /**
 * 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()正确地返回委托:?

2 个答案:

答案 0 :(得分:4)

您是否尝试过定义$ 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;
}

Delegation.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)

因此,参考实际上已经保存并且可以阅读。唷。祝你好运! : - )

答案 1 :(得分:0)

我有一个类似的错误,即我在某些新创建的实体上的多对一关系包含实体,而一对多的逆关系则没有,尽管数据库中显然有对应的行。

我确实坚持并冲洗了一对多实体,但我也必须这样做

$entityManager->clear();

在再次从存储库中获取这些实体以使一对多关系能够访问这些实体之前。