0

我有一个问题,我想使用我查询的第一个实体来加载它关联的一对多实体。我遇到的问题是我想将“$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'"));
4

1 回答 1

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();
}
于 2013-08-03T08:16:08.750 回答