I need to get a random record from the database. How i do it:
$qb = $this->createQueryBuilder('article');
$totalRowsTable = $qb->select('count(article.id)')->getQuery()->getSingleScalarResult();
var_dump($totalRowsTable);
$random = (int)max(0, rand(1, $totalRowsTable));
var_dump($random);
$qb = $this->createQueryBuilder('article')
->andWhere("article.id IN (:id)")
->setParameter('id', $random)
->setMaxResult(1);
return $qb->getQuery()->getResult();
I'm not understanding why the function returns zero. How can I get a random record from the database in a more correct way?