本文介绍了Doctrine2 OneToMany - ManyToOne返回带有数据库的空集合ok的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这种类型的很多关系,但我看不出为什么这个不起作用。

我有一个代表团和一个促销实体:

代表团



促销

  / ** 
* Company\CBundle\Entity\Promotion
*
* @ ORM\Entity
* @ DoctrineAssert\UniqueEntity(promotionCode)
* /
class Promotion
{
const REGISTER_DAYS = 30;
const REGISTER_DISCOUNT = 100;

/ **
* @var整数$ 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 $描述;

/ **
* @ ORM\Column(name =days,type =integer)
* /
protected $ days;

/ **
* @ ORM\Column(name =discount,type =float)
* /
protected $ discount;

/ **
* @ ORM\ManyToOne(targetEntity =委托,inversedBy =促销)
* @ ORM\JoinColumn(name =delegation_id ,referencedColumnName =id)
* /
private $ delegate;

/ **
* @ ORM\ManyToOne(targetEntity =Product,inversedBy =promotion)
* /
private $ product;

/ **
* @var date $ adquiredDate
*
* @ ORM\Column(name =adquiredDate,type =date,可空= true)
* /
private $ adquiredDate;

在控制器中创建一个促销活动时,促销表具有与授权相关的新对象

 私有函数createPromotion($ delegate)
{
$ em = $ this-> getDoctrine ) - > getEntityManager();
$ promotion = Promotion :: createPromotion($ delegacion,Promotion :: REGISTER_DAYS,Promotion :: REGISTER_DISCOUNT);
$ em-> persist($ promotion);
$ em-> persist($ delegate);

$ em-> flush();
}

数据库

  *************************** 15.行*********** **************** 
id:32
委托代码:19
天:20
折扣:50
adquiredDate: 2013-01-10

************************* 16.行******* ********************
id:33
委托代码:19
天:25
折扣:50
adquired日期:2013-01-10
************************* 17.行******* ********************
id:34
委托代码:19
日:30
折扣:50
adquiredDate:2013-01-10

但是当我调用$ delegate-> getPromotions()在另一个控制器/操作中,没有任何促销活动,返回一个没有数据的Doctrine\ORM\PersistentCollection。



可以帮助吗? >


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

解决方案

您是否尝试过定义$代理属性,如

  / ** 
* @ ORM\ManyToOne(targetEntity =代理,inversedBy =促销)
* @ ORM\JoinColumn(name =delegate_id,referencedColumnName =id)
* /
private $ delegate;

请参阅






您的代码中还有很多打字错误。例如

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

mappedBy =delegacion应该是 mappedBy =委托



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

应该是

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



编辑



好的,我为你创造了一个适合我的简约版本。您可以从那里建立或观察与您的代码的不同之处:



Promotion.php

 使用Doctrine\ORM\Mapping作为ORM; 

/ **
* @ ORM\Entity
* /
类促销
{
/ **
* @ ORM\Id
* @ ORM\Column(type =integer)
* @ ORM\GeneratedValue(strategy =AUTO)
* /
protected $ ID;

/ **
* @ ORM\ManyToOne(targetEntity =委托,inversedBy =促销,cascade = {persist})
* @ ORM\\ \\JoinColumn(name =delegate_id,referencedColumnName =id)
* /
public $ delegate;

/ **
* @ ORM\ManyToOne(targetEntity =Product,inversedBy =promotion,cascade = {persist})
* @ ORM\\ \\ JoinColumn(name =product_id,referencedColumnName =id)
* /
public $ product;
}

Deleg.php

 使用Doctrine\ORM\Mapping作为ORM; 

/ **
* @ ORM\Entity
* /
class授权
{
/ **
* @ ORM\Id
* @ ORM\Column(type =integer)
* @ ORM\GeneratedValue(strategy =AUTO)
* /
protected $ ID;

/ **
* @ ORM\OneToMany(targetEntity =Promotion,mappedBy =delegate,cascade = {all},orphanRemoval = true)
* /
public $促销;

public function __construct(){
$ this-> promotion = new \Doctrine\Common\Collections\ArrayCollection();
}
}

Product.php

 使用Doctrine\ORM\Mapping作为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 $促销;

public function __construct(){
$ this-> promotion = new \Doctrine\Common\Collections\ArrayCollection();
}
}

如果你现在做一些像

  $ delegate = new Delegator(); 
$ product = new Product();
$ promotion = new Promotion();
$ promotion-> delegate = $ delegation;
$ promotion-> product = $ product;

$ em-> persist($ promotion);
$ em-> flush();

$ products = $ em-> createQuery('从BundleName\Entity\Product p'中选择p) - > execute();
$ grants = $ em-> createQuery('从BundleName\Entity\Delegation d'中选择d) - > execute();

var_dump(count($ products [0] - >促销),count($ delegation [0] - >促销));

你应该结束

  int(1)
int(1)

事实上保存并可以阅读。唷。祝你好运! : - )



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

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.

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 :?

解决方案

Have you tried defining your $delegation property like

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

See 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".

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

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

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! :-)

这篇关于Doctrine2 OneToMany - ManyToOne返回带有数据库的空集合ok的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:34