联接列无法识别实体关系


Entity relationship is not recognied by the join column

我正在尝试在Monthly表中查找与Category有关联的所有结果。我做了研究,下面的代码理论上应该可以工作,但不知何故抛出了以下错误Notice: Undefined index: joinColumns in...

表由一个联接表联接,使用的键是category_idmonthly_id尝试使用category_id运行查询,但仍然不起作用,并引发另一个错误。

当我运行orm:validate-schema时,似乎一切都很好。我错过了什么?

来自月度实体

    /**
     * @var 'Doctrine'Common'Collections'Collection
     *
     * @ORM'ManyToMany(targetEntity="Bnk'Entity'Category", inversedBy="monthly")
     * @ORM'JoinTable(name="months_categories",
     *   joinColumns={
     *     @ORM'JoinColumn(name="monthly_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM'JoinColumn(name="category_id", referencedColumnName="id")
     *   }
     * )
     */
    private $category;

为了找到与给定Category关联的所有Monthly,我缺少了什么?

$months = $this->getEntityManager()
    ->getRepository('Bnk'Entity'Monthly')
    ->findBy(
        array(
            "category"=>$category->getId()
        )
);

您可能忘记初始化ArrayCollection了吗?在实体构造函数中,您总是必须初始化它们。

因此,在你的班级中名列前茅:

use Doctrine'Common'Collections'ArrayCollection;

和你的构造函数:

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