Created
October 20, 2014 18:06
-
-
Save KaduNovoK/e28971675f497cbb799a to your computer and use it in GitHub Desktop.
Exemplo de uso do Query Builder em outras Queries do mesmo Repositório
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 | |
// some code... | |
/** | |
* Query Builder to get all users with age > $age | |
* | |
* @param int $age | |
* | |
* @return Doctrine\ORM\QueryBuilder $qb | |
*/ | |
public function findAllUsersWithAgeGreaterThanQueryBuilder($age) | |
{ | |
$qb = $this->createQueryBuilder('u'); | |
$qb->where('m.age > :age'); | |
$qb->setParameters(array( | |
'age' => $age, | |
)); | |
return $qb; | |
} | |
/** | |
* Get all users with age > $age | |
* | |
* @param int $age | |
* | |
* @return \Doctrine\Common\Collections\ArrayCollection $result | |
*/ | |
public function findAllUsersWithAgeGreaterThan($age) | |
{ | |
$qb = $this->findAllUsersWithAgeGreaterThanQueryBuilder($age); | |
$result = $qb->getQuery()->getResult(); | |
return $result; | |
} | |
/** | |
* Get all users with age > $age and height > $height | |
* | |
* @param int $age | |
* @param int $height | |
* | |
* @return \Doctrine\Common\Collections\ArrayCollection $result | |
*/ | |
public function findAllUsersWithAgeGreaterThanAndHeightGreaterThan($age, $height) | |
{ | |
$qb = $this->findAllUsersWithAgeGreaterThanQueryBuilder($age); // Code Reuse (it's good) | |
$qb->andWhere('u.height > :height'); | |
$qb->setParameters(array( | |
'height' => $height, | |
)); | |
$result = $qb->getQuery()->getResult(); | |
return $result; | |
} | |
// some code.... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment