1
votes

I look for the way to take the smallest id of a city to avoid an error in my unitarian test. After try, I have this error:

Doctrine\ORM\Query\QueryException: [Syntax Error] line 0, col 62:

Error: Expected =, <, <=, <>, >, >=, !=, got 'AS'

public function testDB()
{
    $id = $this->entityManager
        ->getRepository('AppBundle:City')
        ->createQueryBuilder('b')
        ->where("MIN(b.id) AS id")
        ->getQuery()
        ->getResult();
}
2

2 Answers

3
votes

you can try this:

$qb = $this->getEntityManager()->createQueryBuilder();
$id = $qb
        ->select('city.id')
        ->from('AppBundle:City', 'city')
        ->orderBy('city.id', 'ASC')
        ->setMaxResults(1)
        ->getQuery()
        ->getResult();

You can also use getSingleScalarResult instead if getResult to get only one single scalar result

0
votes
$qb = $this->getEntityManager()->createQueryBuilder();
$id = $qb
    ->select('MIN(city.id) AS id')
    ->from('AppBundle:City', 'city');