我有一个实体类:

/**
 * @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
}

它和数据库连接得很好。实际上,此控制器按预期工作:
/**
 * @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;
}

但如果我尝试使用DQL进行查询,如下所示:
/**
 * @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;
}

我得到的是一个Symfony错误:
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...

通过var_转储$query->getResult()命令,我得到的是以下查询,它的引号看起来不正确:
SELECT "0_.username AS username0, "0_.email AS email1, "0_.password AS password2 FROM "app_auth"."User" "0_

注意,我正在使用实体的模式名。我不知道我的DQL有什么问题。有什么帮助吗?我用的是Symfony 2.8lts

最佳答案

只引用表名,不引用架构名:

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

关于postgresql - Doctrine 的DQL无法正确生成查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36825386/

10-13 00:47