Skip to content

Instantly share code, notes, and snippets.

@KaduNovoK
Created October 20, 2014 18:06
Show Gist options
  • Save KaduNovoK/e28971675f497cbb799a to your computer and use it in GitHub Desktop.
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
<?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