在此之前,我尝试了搜索功能,但没有找到关于这个的线程。 继承人我的问题。我正在使用具有多对多关系的doctrine查询构建器。
$qb = $this->getDoctrine()->getRepository('SOMEBUNDLE:INSERTION')->createQueryBuilder('insertion');
$qb->select('
insertion.id,
insertion.title,
insertion.content,
insertion.insertionpicture,
insertion.timestamp,
insertion.isanon,
user.firstname as user_firstname,
user.lastname as user_lastname,
user.picture as user_picture,
supertag.name as supertag_name
');
$qb->from('SOMEBUNDLE:USER', 'user');
$qb->from('SOMEBUNDLE:SUPERTAG', 'supertag');
$qb->from('SOMEBUNDLE:TAG', 'tag');
$qb->andWhere('insertion.user = user.id');
$qb->andWhere('insertion.supertag = supertag.id');
$qb->andWhere("insertion.tag = :tag");
$qb->setParameters('tag', $tags);
return new JsonResponse($qb->getQuery()->getArrayResult());
Insertion.php
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $tag;
这里的“andwhere(insertion.tag =:tag)”我的参数是一个数组。我得到了一个无效的路径表达式异常,因为我不知道如何设置Doctrine集合的参数。
THX
答案 0 :(得分:0)
尝试替换它:
$qb->andWhere("insertion.tag = :tag");
由此:
$qb->andWhere(
$qb->expr()->in('insertion.tag', $tags)
);
无论如何,您可以通过将调用链接到查询构建器来减少代码:
$qb = $this->getDoctrine()
->getRepository('SOMEBUNDLE:INSERTION')
->createQueryBuilder('insertion');
$query = $qb
# select only some columns from the tables
->select('partial insertion.{id, title, content, '.'
insertionpicture, timestamp, isanon}, '.
'partial user.{id, firstname, lastname, picture}, '.
'partial supertag.{id, name} '
)
# if the Doctrine association is correct, joins are easy
->leftJoin('insertion.user', 'user')
->leftJoin('insertion.supertag', 'supertag')
->leftJoin('insertion.tag', 'tag')
->andWhere(
$qb->expr()->in('tag.name', $tags)
);
return new JsonResponse($query->getQuery()->getArrayResult());