如何使用具有比较标准的findBy方法
我需要使用“ magic finder” findBy方法并使用比较标准(不仅是精确标准)。换句话说,我需要执行以下操作:
$result = $purchases_repository->findBy(array("prize" => ">200"));这样我就能获得所有奖金在200以上的商品。
回答:
这是一个使用Expr()类的示例-
我几天前也需要这样做,花了一些时间来找出确切的语法和用法:
/** * 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')
           ->where(
             $qb->expr()->gt('p.price', $price)
           )
           ->orderBy('p.price', 'DESC')
           ->getQuery();
  return $q->getResult();
}
以上是 如何使用具有比较标准的findBy方法 的全部内容, 来源链接: utcz.com/qa/397894.html
