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

hos*_*eio 5 one-to-many symfony doctrine-orm


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

晋升

    /**
 * 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;
Run Code Online (Sandbox Code Playgroud)

在控制器中创建促销时,表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();
}
Run Code Online (Sandbox Code Playgroud)

数据库

*************************** 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
Run Code Online (Sandbox Code Playgroud)

但是,当我在另一个控制器/动作中调用$ delegation-> getPromotions()时,没有提升,则返回没有数据的Doctrine \ ORM \ PersistentCollection。

有人可以帮忙吗?


编辑更多信息。$ delegation-> getPromotions()为空,但是正在寻找对该委托的升级并调用$ promotion-> getDelegation()可以正确返回委托:

flu*_*flu 5

您是否尝试过定义$ delegation属性,例如

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

请参阅Doctrine2文档:关联映射->多对一


您的代码中也有很多错别字。例如

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

mappedBy="delegacion"应该是mappedBy="delegation"

要么

public function getDeleTacion()
{
    return $this->deleTacion;
}
Run Code Online (Sandbox Code Playgroud)

应该

public function getDelegation()
{
    return $this->delegation;
}
Run Code Online (Sandbox Code Playgroud)

编辑

好的,我为您创建了一个对我有用的简约版本。您可以从那里构建它,或者注意代码之间的差异:

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;
}
Run Code Online (Sandbox Code Playgroud)

委托.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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您现在做类似的事情

$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));
Run Code Online (Sandbox Code Playgroud)

你应该以

int(1)
int(1)
Run Code Online (Sandbox Code Playgroud)

因此,实际上已保存了反射并且可以读取。ew 祝你好运!:-)


gvl*_*sov 5

我遇到了类似的错误,其中一些新创建的实体上的多对一关系包含实体,而反向一对多则不包含实体,尽管数据库中显然有相应的行。

\n\n

我确实在一对多方面坚持并刷新了实体,但我还必须执行 \xc2\xa0

\n\n
$entityManager->clear();\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n