Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DQL query produces error Expected Literal, got 'BY'

I am having a problem with a DQL query to retrieve users and roles from a MySql database. I am using Zend Framework 2 and Doctrine 2.

The query is as follows.

public function getUsers()
{
    $builder = $this->getEntityManager()->createQueryBuilder();
    $builder->select('u, r')
            ->from('Application\Entity\Users', 'u')
            ->leftJoin('Application\Entity\UserRoles', 'r')
            ->orderBy("u.emailAddress", "ASC");

    InfoLogger::vardump($builder->getDQL());

    return $builder->getQuery()->getResult(Query::HYDRATE_OBJECT);
}

The above query produces the error, [Syntax Error] line 0, col 91: Error: Expected Literal, got 'BY'

The generated DQL is

SELECT u, r FROM Application\Entity\Users u LEFT JOIN Application\Entity\UserRoles r ORDER BY u.emailAddress ASC

Can someone please spot what is wrong with this query, many thanks.

like image 643
Garry Avatar asked Feb 11 '15 17:02

Garry


1 Answers

I have figured it out.

The problem was with the line

->leftJoin('Application\Entity\UserRoles', 'r')

This should have been

->leftJoin('u.userRole', 'r')

userRole is defined in my Entity\Users class.

like image 163
Garry Avatar answered Oct 16 '22 07:10

Garry