Yvon PLANCADE
Yvon PLANCADE

Reputation: 107

doctrine querybuilder $qb -> expr()-> all

I'm working with Symfony2. I have to list last $nbOpe entities of Entity OpeEntetes. The following script gives me error:

Blockquote

[Doctrine\ORM\Query\QueryException] [Semantical Error] line 0, col 16 near 'ALL(SELECT FROM': Error: Class 'ALL' is not defined.

Blockquote

What to do ?

public function essai1($nbOpe){
        $in = $this->_em->createQueryBuilder()
            ->from('GcRequetesBundle:OpeEntetes','ein')
            ->orderBy('ein.oeNumOpe', 'DESC')
            ->setMaxResults($nbOpe);

$qb = $this->_em->createQueryBuilder();
            $qb
                -> add("select",new Expr\Select(array('res')))
                -> add('from', new Expr\From($qb -> expr()-> all($in -> getDql()), 'res'));
            $query = $qb -> getQuery();
            $result= $query -> getArrayResult();
            return $result;
        }

Upvotes: 1

Views: 7513

Answers (1)

ejuhjav
ejuhjav

Reputation: 2710

There are couple of errors in your code:

In any case, the restriction that subqueries do not support the setMaxLimits probably means that you cannot build your code in this way at all. If that isn't a show-stopper for you, then you could try it with the following code for example (easiest would be to just have one query, but I assume you have some reason for doing this in two steps...):

    $in = $em->createQueryBuilder()
        ->select('ein')
        ->from('GcRequetesBundle:OpeEntetes','ein')
        ->orderBy('ein.oeNumOpe', 'DESC')
        ->setMaxResults($nbOpe); // this does not affect the result at all

    $qb = $em->createQueryBuilder();
    $qb->add('select', 'ein2');
    $qb->add('from', 'GcRequetesBundle:OpeEntetes ein2');
    $qb->add('where', $qb->expr()->eq('ein2.id', $qb->expr()->any($in->getDql())));
    $query = $qb -> getQuery();
    $result= $query -> getArrayResult();
    return $result;

Upvotes: 1

Related Questions