(정확한 기준뿐만 아니라) 비교 기준을 사용하여 “매직 파인더” findBy 방법을 사용해야 합니다.즉, 다음과 같은 작업을 수행해야 합니다.
$result = $purchases_repository->findBy(array("prize" => ">200"));
200점 이상 구매하실 수 있도록요.
질문에 대한 답변
클래스는 API를 구현합니다.
그Selectable
인터페이스는 매우 유연하고 매우 새로운 것이지만, ORM 또는 ODM 또는 완전히 다른 문제에 관계없이 저장소 및 단일 항목 컬렉션 모두에서 비교 및 보다 복잡한 기준을 쉽게 처리할 수 있습니다.
이는 방금 요청하신 바와 같이 교리 ORM에서와 같은 비교 기준이 됩니다.2.3.2
:
$criteria = new DoctrineCommonCollectionsCriteria(); $criteria->where(DoctrineCommonCollectionsCriteria::expr()->gt('prize', 200));
$result = $entityRepository->matching($criteria);
이 API의 주요 장점은 여기서 일종의 전략 패턴을 구현하고 있으며 저장소, 컬렉션, 느린 컬렉션 및 모든 장소에서 사용할 수 있다는 것입니다.Selectable
API가 구현되어 있습니다.
이를 통해 저장소용으로 작성한 수십 가지 특별한 방법을 제거할 수 있습니다(예:findOneBySomethingWithParticularRule
대신, 각각 이러한 특정 필터 중 하나를 나타내는 자체 기준 클래스를 작성하는 데 초점을 맞춥니다.
이 예는 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(); }
DQL 또는 QueryBuilder 중 하나를 사용해야 합니다.예: Purchase-EntityRepository에서는 다음과 같은 작업을 수행할 수 있습니다.
$q = $this->createQueryBuilder('p')
->where('p.prize > :purchasePrize')
->setParameter('purchasePrize', 200)
->getQuery();
$q->getResult();
더 복잡한 시나리오에 대해서는 Expr() 클래스를 참조하십시오.
$criteria = new DoctrineCommonCollectionsCriteria();
$criteria->where($criteria->expr()->gt('id', 'id'))
->setMaxResults(1)
->orderBy(array("id" => $criteria::DESC));
$results = $articlesRepo->matching($criteria);
이제 Symfony 문서에는 이 작업을 수행하는 방법이 명확하게 나와 있습니다.
$em = $this->getDoctrine()->getManager(); $query = $em->createQuery(
'SELECT p
FROM AppBundle:Product p
WHERE p.price > :price
ORDER BY p.price ASC' )->setParameter('price', '19.99');
$products = $query->getResult();
http://symfony.com/doc/2.8/book/doctrine.html#querying-for-objects-with-dql 에서