是否可以添加到现有的Doctrine查询?

时间:2016-04-29 16:00:39

标签: php symfony doctrine-orm query-builder

我在Symfony中构建了一个页面,它利用Doctrine Query Builder返回结果。该页面还集成了搜索功能。页面工作正常,但我想知道我的代码的Doctrine Query Builder部分是否可以重新计算?

所以我的问题是,而不是复制和粘贴相同的查询(就像我在下面的代码($ results)中所做的那样)并添加额外的'其中'和' setParameter'查询构建器的方法,是否可以将其他选项附加到第一个查询,以便我不必重新使用代码?

$em = $this->getDoctrine()->getManager('managerName');
$results = $em->createQueryBuilder()
       ->select('d.name','d.createdAt')
       ->from('ManagerName:Demo', 'd')
       ->orderBy('d.name')
       ->getQuery()
       ->getResult();

if ($form->isSubmitted() && $form->isValid()) {
    if (isset($_POST['submit'])) {
        $em = $this->getDoctrine()->getManager('managerName');
        $results = $em->createQueryBuilder()
              ->select('d.name', 'd.createdAt')
              ->from('ManagerName:Demo', 'd')
              ->where('d.name LIKE :searchTerm')
              ->orderBy('d.name')
              ->setParameter('searchTerm', '%' . $search->getSearch() . '%')
              ->getQuery()
              ->getResult();
            return $this->render('AcmeBundleName:Demo:index.html.twig', [
                "form" => $form->createView(),
                "results" => $results,
                "search" => $search->getSearch(),
            ]);
        }
    return $this->redirect($request->getUri());
}

所以我正在寻找/试图获得的一个例子如下:

$em = $this->getDoctrine()->getManager('managerName');

$query = $em->createQueryBuilder()
       ->select('d.name','d.createdAt')
       ->from('ManagerName:Demo', 'd')
       ->orderBy('d.name')
       ->getQuery();

$results = $query->getResult();

if ($form->isSubmitted() && $form->isValid()) {
    if (isset($_POST['submit'])) {                
        $em = $this->getDoctrine()->getManager('managerName');

        $results = $query->where('d.name LIKE :searchTerm')
               ->setParameter('searchTerm', '%' . $search->getSearch() . '%');

        return $this->render('AcmeBundleName:Demo:index.html.twig', [
                "form" => $form->createView(),
                "results" => $results,
                "search" => $search->getSearch(),
            ]);
    }
    return $this->redirect($request->getUri());
}

我不知道是否有可能扩展我之前构建的查询和谷歌搜索几个小时似乎没有返回任何有用的来源。

我还浏览了大量的Doctrine文档,包括以下内容但没有成功:http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.QueryBuilder.html

提前致谢

2 个答案:

答案 0 :(得分:1)

在这种情况下,您只需在执行请求时调用getQuery()

$em = $this->getDoctrine()->getManager('managerName');

$query = $em->createQueryBuilder()
        ->select('d.name','d.createdAt')
        ->from('ManagerName:Demo', 'd')
        ->orderBy('d.name');

$results = $query
    ->getQuery()
    ->getResult();

if ($form->isSubmitted() && $form->isValid()) {
    if (isset($_POST['submit'])) {                
        $results = $query->where('d.name LIKE :searchTerm')
                ->setParameter('searchTerm', '%' . $search->getSearch() . '%')
                ->getQuery()
                ->getResult();

        return $this->render('AcmeBundleName:Demo:index.html.twig', [
            "form" => $form->createView(),
            "results" => $results,
            "search" => $search->getSearch(),
        ]);
    }
    return $this->redirect($request->getUri());
}

$query\Doctrine\ORM\QueryBuilder的一个实例,您可以在此对象上调用where()等。

然后您需要致电$query->getQuery()->getResult();来执行查询。

请注意,我也删除了这个重复的行:

$em = $this->getDoctrine()->getManager('managerName');

答案 1 :(得分:0)

当然可以。如果你单独使用querybuilder对象,这是相当简单的。

$qb = $em->createQueryBuilder()
        ->select('d.name','d.createdAt')
        ->from('ManagerName:Demo', 'd')
        ->orderBy('d.name');

$query = $qb->getQuery();