我有一个问题,我想使用我查询的第一个实体加载它关联的一个到多个实体。我遇到的问题是我想将“$ campaignLogs”的查询限制为“status”==“public”
以下示例不起作用,但显示了我想要做的事情。我已经发现你不能在数组集合上使用where(),因为它已经等价地设置或处理了查询。
$em = $this->getDoctrine()->getManager();
$campaign = $em->getRepository('STLDevRPGBundle:Campaign\Campaign')->findOneByTitle($slug);
// This is what doesn't work, I have to change the "get" somehow
// or does this have to be a custom repository function?
$campaignLogs = $campaign->getCampaignLogs()->where(array("status = 'public'"));
答案 0 :(得分:1)
您可以为现有的自定义方法添加可选参数。做这样的事情:
public function findOneByTitle($slug, $status = null)
{
$qb = $this->createQueryBuilder('c');
$qb->select('c');
->where('c.title = :title')
->setParameter('title', $slug);
if ($status !== null) {
$qb->join('c.campaignLogs', 'l');
$qb->where('l.status = :status')
->setParameter('status', $status);
}
return $qb->getQuery()->getSingleResult();
}