教义.为什么我在ManyToMany上得到了persistentCollection和一个空数组? [英] Doctrine. Why i get persistentCollection and an empty array on ManyToMany?

查看:93
本文介绍了教义.为什么我在ManyToMany上得到了persistentCollection和一个空数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的实体:

/**
* Productgeneral
* @ORM\Table(name="ProductGeneral", indexes={@ORM\Index(name="category_id", columns={"category_id"})})
* @ORM\Entity
*/
class Productgeneral {

    //some cols

    /**
     * @var integer
     *
     * @ORM\Column(name="product_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $productId;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Productimg", inversedBy="product")
     * @ORM\JoinTable(name="producttoimg",
     *   joinColumns={
     *     @ORM\JoinColumn(name="product_id", referencedColumnName="product_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="img_id", referencedColumnName="img_id")
     *   }
     * )
     */
    private $img;

    /**
     * Constructor
     */
    public function __construct() {
        $this->img = new \Doctrine\Common\Collections\ArrayCollection();
    }

    //getter & setters
}

/**
 * Productimg
 * @ORM\Table(name="ProductImg")
 * @ORM\Entity
 */
class Productimg {
    /**
     * @var integer
     *
     * @ORM\Column(name="img_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $imgId;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Productgeneral", mappedBy="img")
     */
    private $product;

    /**
     * Constructor
     */
    public function __construct() {
        $this->product = new \Doctrine\Common\Collections\ArrayCollection();
    }

    //getters & setters
}

当我跑步时:

*$this->getDoctrine()->getRepository('AppBundle:Productgeneral')->findAll();*

我得到的每一列的值都正确,但是img;会返回PersistentCollection而不是所有图像的数组.

我做错什么了吗?还是我只是误解了这种关系的行为?

解决方案

默认的行为是延迟获取映射的实体,因此,当您转储您的实体时,它似乎为空,因为尚未加载数据. /p>

如果调用getImg方法,则该学说将查询数据库以加载链接到产品的相关Productimg实体.

一旦ArrayCollection由实体管理器持久保存和管理,它将变为PersistentCollection,其行为完全具有ArrayCollection

This is my Entity:

/**
* Productgeneral
* @ORM\Table(name="ProductGeneral", indexes={@ORM\Index(name="category_id", columns={"category_id"})})
* @ORM\Entity
*/
class Productgeneral {

    //some cols

    /**
     * @var integer
     *
     * @ORM\Column(name="product_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $productId;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Productimg", inversedBy="product")
     * @ORM\JoinTable(name="producttoimg",
     *   joinColumns={
     *     @ORM\JoinColumn(name="product_id", referencedColumnName="product_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="img_id", referencedColumnName="img_id")
     *   }
     * )
     */
    private $img;

    /**
     * Constructor
     */
    public function __construct() {
        $this->img = new \Doctrine\Common\Collections\ArrayCollection();
    }

    //getter & setters
}

/**
 * Productimg
 * @ORM\Table(name="ProductImg")
 * @ORM\Entity
 */
class Productimg {
    /**
     * @var integer
     *
     * @ORM\Column(name="img_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $imgId;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Productgeneral", mappedBy="img")
     */
    private $product;

    /**
     * Constructor
     */
    public function __construct() {
        $this->product = new \Doctrine\Common\Collections\ArrayCollection();
    }

    //getters & setters
}

When I run:

*$this->getDoctrine()->getRepository('AppBundle:Productgeneral')->findAll();*

I get every column with its correct value but img; which returns a PersistentCollection instead of an array of all the images.

Am I doing something wrong? Or have I just misunderstand the behavior of the relationship?

解决方案

The default Doctrine behaviour is to lazy fetch mapped entities so when you make a dump of your entity it appears to be null because the data have not been loaded.

If you call the getImg method then doctrine will query your database to load the related Productimg entities linked to your product.

Once an ArrayCollection is persisted and managed by the entity manager it becomes a PersistentCollection it behaves exactly has an ArrayCollection

这篇关于教义.为什么我在ManyToMany上得到了persistentCollection和一个空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆