o5snag
Last Updated: February 25, 2016
·
6.867K
· stefanosala
43685b9e08bb8bd76b222dd1c6977312

Add a HIDDEN select column to Doctrine query builder

Just add HIDDEN keyword to select:

<?php
namespace Acme\FooBundle\Repository;

use Doctrine\ORM\EntityRepository;

class FooRepository extends EntityRepository
{
    public function findRandom($number)
    {
        return $this->createQueryBuilder('q')
            ->select('q, RAND() as HIDDEN r')
            ->setMaxResults($number)
            ->orderBy('r')
            ->getQuery()
            ->execute();
    }
}
Say Thanks
Respond

1 Response
Add your response

3439
43685b9e08bb8bd76b222dd1c6977312

Yeah, I know RAND() is not the best way to select random rows :)

over 1 year ago ·