DQL query produces error Expected Literal, got 'BY'
Asked Answered
A

1

6

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.

Affaire answered 11/2, 2015 at 17:30 Comment(0)
A
8

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.

Affaire answered 11/2, 2015 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.