The class DoctrineORMEntityRepository
implements DoctrineCommonCollectionsSelectable
API.
The Selectable
interface is very flexible and quite new, but it will allow you to handle comparisons and more complex criteria easily on both repositories and single collections of items, regardless if in ORM or ODM or completely separate problems.
This would be a comparison criteria as you just requested as in Doctrine ORM 2.3.2
:
$criteria = new DoctrineCommonCollectionsCriteria();
$criteria->where(DoctrineCommonCollectionsCriteria::expr()->gt('prize', 200));
$result = $entityRepository->matching($criteria);
The major advantage in this API is that you are implementing some sort of strategy pattern here, and it works with repositories, collections, lazy collections and everywhere the Selectable
API is implemented.
This allows you to get rid of dozens of special methods you wrote for your repositories (like findOneBySomethingWithParticularRule
), and instead focus on writing your own criteria classes, each representing one of these particular filters.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…