DannyDanDan DannyDanDan - 7 months ago 22
PHP Question

Is it possible to add on to an existing Doctrine Query?

I've built a page in Symfony, which utilises the Doctrine Query Builder to return results. The page also integrates search functionality. The page works fine at the minute, but I'm wondering if the Doctrine Query Builder section of my code can be re-factored?

So my question is, rather than copying and pasting the same query (as I have done so below in my code ($results)) and adding additional 'where' and 'setParameter' methods to the query builder, is it possible to just append the additional options to the first query so that I don't have to re-use code?

$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());
}


So an example of what I'm looking for/trying to get at would be like the following:

$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());
}


I've no idea if its possible to extend the query I've previously built and Googling for hours on end doesn't seem to return any useful sources I can relate to.

I've also looked through plenty of Doctrine documentation, including the following without success: http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.QueryBuilder.html

Thanks in advance

A.L A.L
Answer

In this case you need to call getQuery() only when you perform the request:

$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 is an instance of \Doctrine\ORM\QueryBuilder and you can call where(), etc. on this object.

Then you need to call $query->getQuery()->getResult(); to perform the query.

Note that I also remove this duplicated line:

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