Last active
August 29, 2015 14:11
-
-
Save fabiopaiva/067a46e8ee82a2dd7980 to your computer and use it in GitHub Desktop.
Find person using LIKE command in multiple fields with Doctrine2 and ZF2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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