简体   繁体   English

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

[英]Doctrine2 OneToMany - ManyToOne returns empty collection with database ok


I have many relations of this type, but I can't see why this one is not working. 我有很多这种类型的关系,但我不明白为什么这个关系不起作用。
I have a Delegation and a Promotion entities: 我有一个代表团和一个促销实体:
Delegation 代表团

Promotion 提升

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

When in a controller I create a promotion, the table Promotion has the new object related to the delegation one 当我在控制器中创建一个促销时,表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();
}

Database 数据库

*************************** 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

But when I call the $delegation->getPromotions() in another controller/action there is no promotions, returns a Doctrine\\ORM\\PersistentCollection with no data. 但是当我在另一个控制器/操作中调用$ delegation-> getPromotions()时没有促销,返回没有数据的Doctrine \\ ORM \\ PersistentCollection。

Can anyone help, please? 有人可以帮忙吗?


Edit with more information. 编辑更多信息。 $delegation->getPromotions() is empty, but looking for a promotion of that delegation and calling $promotion->getDelegation() is returning the delegation correctly :? $ delegation-> getPromotions()是空的,但是寻找促销该委托并调用$ promotion-> getDelegation()正确地返回委托:

Have you tried defining your $delegation property like 您是否尝试过定义$ delegation属性

/**
 * @ORM\ManyToOne(targetEntity="Delegation", inversedBy="promotions")
 * @ORM\JoinColumn(name="delegation_id", referencedColumnName="id")
 */
private $delegation;

See Doctrine2 Docs: Association Mapping->Many-To-One 请参阅Doctrine2 Docs:Association Mapping-> Many-To-One


Also there are a lot of typos in your code. 你的代码中也有很多拼写错误。 For example 例如

/**
 * @ORM\OneToMany(targetEntity="Promotion", mappedBy="delegacion", cascade={"all"}, orphanRemoval=true)
 */
protected $promotions;

mappedBy="delegacion" should be mappedBy="delegation" . mappedBy="delegacion"应该是mappedBy="delegation"

Or 要么

public function getDeleTacion()
{
    return $this->deleTacion;
}

Should be 应该

public function getDelegation()
{
    return $this->delegation;
}

Edit 编辑

Okay, I created a minimalistic version for you that worked for me. 好的,我为你创建了一个适合我的简约版本。 You can built it up from there or watch for differences with your code: 您可以从那里构建它或者注意与代码的差异:

Promotion.php 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 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 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();
    }
}

If you now do something like 如果你现在做的事情

$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));

You should end up with 你应该最终得到

int(1)
int(1)

So the refrence is in fact saved and can be read. 因此,参考实际上已经保存并且可以阅读。 Phew. 唷。 Good luck with that! 祝你好运! :-) :-)

I had a similar error where my many-to-one relationship on some freshly created entities contained entities, and an inverse one-to-many didn't, although database clearly had the corresponding rows in it. 我有一个类似的错误,我在一些新创建的实体上的多对一关系包含实体,而反对一对多没有,尽管数据库显然有相应的行。

I did persist and flush entities on the one-to-many side, but I had to also do 我确实坚持并在一对多方面冲洗实体,但我也必须这样做

$entityManager->clear();

before getting those entities from a repository again for the one-to-many relationship to be able to access those entities. 在从存储库中再次获取这些实体以获得一对多关系之前,能够访问这些实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM