我有一个实体Image,其属性为“categories”。 Categories是Doctrine的ArrayCollection的一个实例,它包含不同的Category对象。
我现在想要获取包含Category对象“main”的所有Image对象。
使用普通属性时,如下所示:
$repository->findBy(array('category' => 'main'));
这也适用于Array属性,还是我必须通过Category类获取图像?
问候!
答案 0 :(得分:1)
->findBy()
方法仅适用于关系的拥有方。
您还可以在您的存储库中创建自定义方法:
public function findByCategoryName($categoryName)
{
return $this
->createQueryBuilder('image')
->innerJoin('image.categories', 'category')
->where('category.name = :categoryName')
->setParameter('categoryName', $categoryName)
->getQuery()
->getResult()
;
}