我正在尝试在Monthly
表中查找与Category
有关联的所有结果。我做了研究,下面的代码理论上应该可以工作,但不知何故抛出了以下错误Notice: Undefined index: joinColumns in...
表由一个联接表联接,使用的键是category_id
,monthly_id
尝试使用category_id
运行查询,但仍然不起作用,并引发另一个错误。
当我运行orm:validate-schema
时,似乎一切都很好。我错过了什么?
来自月度实体
/**
* @var DoctrineCommonCollectionsCollection
*
* @ORMManyToMany(targetEntity="BnkEntityCategory", inversedBy="monthly")
* @ORMJoinTable(name="months_categories",
* joinColumns={
* @ORMJoinColumn(name="monthly_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORMJoinColumn(name="category_id", referencedColumnName="id")
* }
* )
*/
private $category;
为了找到与给定Category
关联的所有Monthly
,我缺少了什么?
$months = $this->getEntityManager()
->getRepository('BnkEntityMonthly')
->findBy(
array(
"category"=>$category->getId()
)
);
您可能忘记初始化ArrayCollection了吗?在实体构造函数中,您总是必须初始化它们。
因此,在你的班级中名列前茅:
use DoctrineCommonCollectionsArrayCollection;
和你的构造函数:
public function __construct()
{
$this->category= new ArrayCollection();
}