Search code examples
postgresqlsymfonydoctrine-ormdoctrinedql

Doctrine's DQL does not generate the query correctly


I have an entity class:

/**
 * @ORM\Entity
 * @ORM\Table(name="""app_auth"".""User""", schema="app_auth")
 */
class User {
    /**
     * @ORM\Column(type="string")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $username;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private $email;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private $password;

    // removed setters and getters
}

which is perfectly tied to the DB. In fact, this controller works as expected:

/**
 * @Route("users/{username}",
 *     defaults={
 *      "_format" = "json"
 *     })
 * @Method("GET")
 */
public function getUserAction($username)
{ 
    $user = $this->getDoctrine()
        ->getRepository('BelkaTestBundle:User')
        ->find($username);

    if(!$user)
        throw $this->createNotFoundException("User $username not found");

    $res = new JsonResponse();
    $res->setData(array(
        'user' => $user->getUsername(),
        'email' => $user->getEmail()));

    return $res;
}

But if I try to query by means of DQL, like this:

/**
 * @Route("users/{username}",
 *     defaults={
 *      "_format" = "json",
 *     })
 * @Method("GET")
 */
public function getUserAction($username)
{
    $repository = $this->getDoctrine()
            ->getRepository('BelkaTestBundle:User');
    $query = $repository->createQueryBuilder('u');
    $users = $query->getResult();

    /*something here to manage and return the results*/
    return $res;
}

What I get is a Symfony error:

An exception occurred while executing 'SELECT "0_.username AS username0, "0_.email AS email1, "0_.password AS password2 FROM "app_auth"."User" "0_':

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "0"
LINE 1: SELECT "0_.username AS username0, "0_.email AS email1, "0_.p...

By var_dumping the $query->getResult() command, what I got is the following query, which looks bad quoted:

SELECT "0_.username AS username0, "0_.email AS email1, "0_.password AS password2 FROM "app_auth"."User" "0_

Notice I'm using the schema name for my entity. I can't figure out what's wrong with my DQL. Any help? I'm using Symfony 2.8 LTS


Solution

  • Quote only the table name and leave the schema name unquoted:

    * @ORM\Table(name="app_auth.""User""", schema="app_auth")