Symfony 多对多返回空的 ArrayCollection

问题描述

我有两个实体 Product 和 Part,通过多对多关系连接。

class Part
{
/**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Product",mappedBy="parts",fetch="EAGER")
     */
    private $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    /**
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getProducts()
    {
        return $this->products;
    }
}
class Product
{
/**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Part",inversedBy="products",cascade={"persist"})
     * @ORM\JoinTable(name="products_parts",*   joinColumns={
     *     @ORM\JoinColumn(name="product_id",referencedColumnName="id")
     *   },*   inverseJoinColumns={
     *     @ORM\JoinColumn(name="part_id",referencedColumnName="id")
     *   }
     * )
     */
    private $parts;

    public function __construct()
    {
        $this->parts = new ArrayCollection();
    }

    /**
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getParts()
    {
        return $this->parts;
    }

    /**
     * @param $parts
     */
    public function setParts($parts)
    {
        $this->parts = $parts;
    }
}

当我尝试获取单个产品并显示所有相关部件时。 Symfony 返回一个空数组。尽管数据已正确添加到数据库中。我哪里做错了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)