ElP*_*ter 72 php symfony doctrine-orm
我需要使用比较标准的"魔术查找器"findBy方法(不仅仅是准确的标准).换句话说,我需要做这样的事情:
$result = $purchases_repository->findBy(array("prize" => ">200"));
Run Code Online (Sandbox Code Playgroud)
这样我就可以获得奖金超过200的所有购买.
Ocr*_*ius 181
该类Doctrine\ORM\EntityRepository实现Doctrine\Common\Collections\SelectableAPI.
该Selectable接口是非常灵活,很新,但它可以让你在两个存储库和项目的单集轻松地处理比较多的复杂的标准,如果ORM或ODM或完全独立的问题不管.
这将是您在Doctrine ORM中所要求的比较标准2.3.2:
$criteria = new \Doctrine\Common\Collections\Criteria();
$criteria->where($criteria->expr()->gt('prize', 200));
$result = $entityRepository->matching($criteria);
Run Code Online (Sandbox Code Playgroud)
此API的主要优点是您在此处实现某种策略模式,它适用于存储库,集合,惰性集合以及SelectableAPI实现的所有位置.
这允许您摆脱为存储库编写的许多特殊方法(如findOneBySomethingWithParticularRule),而是专注于编写自己的条件类,每个类代表一个特定的过滤器.
con*_*con 30
这是一个使用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();
}
Run Code Online (Sandbox Code Playgroud)
您必须使用DQL或QueryBuilder.例如,在您的Purchase- EntityRepository中,您可以执行以下操作:
$q = $this->createQueryBuilder('p')
->where('p.prize > :purchasePrize')
->setParameter('purchasePrize', 200)
->getQuery();
$q->getResult();
Run Code Online (Sandbox Code Playgroud)
对于更复杂的场景,请查看Expr()类.
小智 6
$criteria = new \Doctrine\Common\Collections\Criteria();
$criteria->where($criteria->expr()->gt('id', 'id'))
->setMaxResults(1)
->orderBy(array("id" => $criteria::DESC));
$results = $articlesRepo->matching($criteria);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93588 次 |
| 最近记录: |