Doctrine ORM – doctrine 2: write an appropriate subselect

I try to get the total number of records $QB will return before I apply for start & Query the limit attribute of $QB My $QB and $totalqb work well on their own, but when I try to use $QB as a sub option, I get an error thrown:

$qb = $this->entityManager->createQueryBuilder()
        ->select('w,se')
        ->from('Dashboard\Entity\Section','se')
        ->innerJoin('se.word','w')
        ->innerJoin('se.location','l');

    $qb->add('where',$qb->expr()->andx(
        $qb->expr()->eq('l.ignored',$ignored),$qb->expr()->eq('l.id',$params['l_id'])
    ),true);

Everything above this line is good

$totalQb = $this->entityManager->createQueryBuilder()
        ->select('COUNT(x.id)')
        ->from('Dashboard\Entity\Section','x');

The above $totalqb itself works well But when I do this, the following and try to use $QB as a sub option of $totalqb

$dql = $qb->getDql();
$totalQb->add('where',$totalQb->expr()->exists( $dql ));
$totalsql = $totalQb->getQuery();

$sql = $totalsql->getsql();
$total = $totalsql->getSingleScalarResult();

This is wrong

It refers to the subselect 'exists' statement But when I run $DQL myself, it returns the expected results

[Syntax Error] line 0,col 69: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM,got ','

What is the correct way to make sub selection in my scenario?

Update someone suggested that I add $totalsql - > getdql(); This is the output of the Declaration:

SELECT COUNT(x.id) FROM Dashboard\Entity\Section x WHERE 
EXISTS(
    SELECT w,se 
    FROM Dashboard\Entity\Section se 
    INNER JOIN se.word w 
    INNER JOIN se.location l 
    AND (l.ignored = 0) 
    AND (l.id = 2) 
    GROUP BY w.id,l.id
)

Solution

I can fix the above query by changing the internal – > selection to pull from only one table

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>