I have 2 entities/tables:
News
- id
- company_id
NewsReader
- id
- news_id
- reader_id
I want to get count unread news for current user.
I write query with query builder:
$companyId = 1;
$userId = 1;
$qb = $this->_em->createQueryBuilder();
$qb->from(News::class, 'n');
$qb->select($qb->expr()->count('n.id'))
->leftJoin(
NewsReader::class,
'nr',
\Doctrine\ORM\Query\Expr\Join::ON,
'nr.news_id = n.id AND nr.reader_id = :userId'
)
->andWhere('n.company_id = :companyId')
->andWhere('nr.id IS NULL')
->setParameter('companyId', $companyId)
->setParameter('userId', $userId);
$query = $qb->getQuery();
return (int)$query->getSingleScalarResult();
DQL is there:
SELECT
COUNT(n.id)
FROM App\Entity\News n
LEFT JOIN App\Entity\NewsReader nr
ON nr.news_id = n.id AND nr.reader_id = :userId
WHERE n.company_id = :companyId AND nr.id IS NULL
But i've got an error:
[Syntax Error] line 0, col 77: Error: Expected end of string, got 'ON'
Whats wrong?
If i write raw query to database in console - all works correctly.