ответаУчение: QueryBuilder Где Exists
Я был в состоянии сделать запрос с использованием НЕ ПУСТЫЕ в ИНЕКЕ.
/**
* Finds all developments having at least one image.
*
* @param string
* @return array
*/
public function withImages()
{
return $this->query->createQueryBuilder('d')
->where('d.images IS NOT EMPTY')
->getQuery()
->getResult();
}
Вопрос
Я использую Doctrine ORM. Я хотел бы иметь возможность получать все разработки, имеющие хотя бы один образ, так что для каждого выбранного в запросе развития свойства было бы истинным следующее свойство. $development->getImages()->count()) > 0
.
У меня есть объект развития, который имеет отношение «Один ко многим» с объектом «Изображение».
/**
* The Development class provides an abstraction of a development.
*
* @Entity
* @HasLifecycleCallbacks
* @Table(name="developments")
**/
class Development extends BaseEntitiy {
/** @OneToMany(targetEntity="Exquisite\Entity\Image", mappedBy="development") **/
protected $images;
У меня есть DevelopmentRepository, который имеет экземпляр EntityManager и экземпляр Repository для объекта. Я попытался сделать это в моем методе withImages() в классе DevelopmentRepository, но не имел большой удачи.
class DevelopmentRepository implements DevelopmentRepositoryInterface {
/**
* Class Constructor
*
* @param Doctinre\ORM\EntityManager The EntityManager instance.
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
$this->query = $this->em->getRepository('Exquisite\Entity\Development');
}
/**
* Finds all developments having at least one image.
*
* @param string
* @return array
*/
public function withImages()
{
$qb = $this->query->createQueryBuilder('d');
$qb2 = $this->em->createQueryBuilder();
return $qb->where($qb->expr()->exists($qb2->select('i.development')->from('Exquisite\Entity\Image', 'i')->getDql()))
->getQuery()
->getResult();
}
Любая помощь будет очень признательна!