Doctrine:QueryBuilder存在的地方

Answer

I was able to do the query using IS NOT EMPTY in the where clause.

/**
 * 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();
}

Question

I am using the Doctrine ORM. I would like to be able to get all developments which have at least one image, such that for every Development selected in the query, the following property would be true. $development->getImages()->count()) > 0.

I have a Development entity which has a One to Many relationship with an Image entity.

/**
 * 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;

I have a DevelopmentRepository which has an instance of a EntityManager and an instance of the Repository for the Entity. I have made an attempt to do this in my withImages() method in the DevelopmentRepository class, but not having much luck.

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();
    }

Any help would be much appreciated!

I had the same problem, and this worked for me.

I fixed just doing a join (would return only the orders with items):

return $this->createQueryBuilder('so')
        ->select('so')
        ->join('so.orderItems', 'soi')
        ->getQuery()
        ->getResult();

Or doing a sub query with DQL

SELECT so
FROM Entity\\SalesOrder so
WHERE (SELECT count(soi.id) FROM Entity\\SalesOrderItem soi WHERE soi.salesOrder = so.id ) > 0

I hope this can be helpful