我有一个实体类:
/**
* @ORMEntity
* @ORMTable(name="""app_auth"".""User""", schema="app_auth")
*/
class User {
/**
* @ORMColumn(type="string")
* @ORMId
* @ORMGeneratedValue(strategy="NONE")
*/
private $username;
/**
* @ORMColumn(type="string", nullable=true)
*/
private $email;
/**
* @ORMColumn(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_dumping $query->getResult()
命令,我得到的是以下查询,引用起来看起来很糟糕:
SELECT "0_.username AS username0, "0_.email AS email1, "0_.password AS password2 FROM "app_auth"."User" "0_
请注意,我使用的是实体的架构名称。我无法弄清楚我的 DQL 出了什么问题。有什么帮助吗?我正在使用 Symfony 2.8 LTS
仅引用表名,将架构名称保留为不引号:
* @ORMTable(name="app_auth.""User""", schema="app_auth")