ElPiter ElPiter - 6 months ago 16x
PHP Question

How to use a findBy method with comparative criteria

I'd need to use a "magic finder" findBy method using comparative criteria (not only exact criteria). In other words, I need to do something like this:

$result = $purchases_repository->findBy(array("prize" => ">200"));

so that I'd get all purchases where the prize is above 200.

con con

This is an example using the Expr() Class - I needed this too some days ago and it took me some time to find out what is the exact syntax and way of usage:

 * fetches Products that are more expansive than the given price
 * @param int $price
 * @return array
public function findProductsExpensiveThan($price)
  $em = $this->getEntityManager();
  $qb = $em->createQueryBuilder();

  $q  = $qb->select(array('p'))
           ->from('YourProductBundle:Product', 'p')
             $qb->expr()->gt('p.price', $price)
           ->orderBy('p.price', 'DESC')

  return $q->getResult();