Skip to content

Instantly share code, notes, and snippets.

@fabiopaiva
Last active August 29, 2015 14:11
Show Gist options
  • Save fabiopaiva/067a46e8ee82a2dd7980 to your computer and use it in GitHub Desktop.
Save fabiopaiva/067a46e8ee82a2dd7980 to your computer and use it in GitHub Desktop.
Find person using LIKE command in multiple fields with Doctrine2 and ZF2
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class PersonController extends AbstractActionController{
public function searchAction(){
$term = $this->params()->fromQuery('term', '');
$qb = $this
->getServiceLocator()
->get('Doctrine\ORM\EntityManager')
->getRepository('Application\Entity\Person')
->createQueryBuilder('q');
$qb
->orWhere($qb->expr()->like('q.name', $qb->expr()->literal("%{$term}%")))
->orWhere($qb->expr()->like('q.alias', $qb->expr()->literal("%{$term}%")))
->orderBy('q.name', 'ASC')
->setMaxResults(20);
$persons = $qb->getQuery()->getResult();
return array(
'persons' => $persons
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment