Doctrine 的 DQL 没有正确生成查询
Doctrine's DQL does not generate the query correctly
我有一个 实体 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
}
与数据库完美绑定。事实上,这个控制器按预期工作:
/**
* @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
仅引用 table 名称,不引用架构名称:
* @ORM\Table(name="app_auth.""User""", schema="app_auth")
我有一个 实体 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
}
与数据库完美绑定。事实上,这个控制器按预期工作:
/**
* @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
仅引用 table 名称,不引用架构名称:
* @ORM\Table(name="app_auth.""User""", schema="app_auth")