SQL 将 DQL 与 symfony 一起使用时出错
SQL error when using DQL with symfony
我正在使用 symfony 框架,当我尝试使用此 DQL 方法为 FOSUser 包
生成的 Fos_user table 恢复用户名时
namespace UserBundle\Repository;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function findEmailDQL($email)
{
$query=$this->getEntityManager()
->createQuery("SELECT u.username
FROM UserBundle:User u
WHERE u.id = $email");
return $query->getResult();
}
}
具有控制器功能
public function findByEmailJsonAction($email)
{
$users=$this->getDoctrine()->getManager()
->getRepository('UserBundle:User')
->findEmailDQL($email);
$serializer = new Serializer([new ObjectNormalizer()]);
$formatted =$serializer->normalize($users);
return new JsonResponse($formatted);
}
当我尝试查找 ID 为 127 的用户时,我不断收到此错误:
执行时出现异常'SELECT u0_.username AS username_0 FROM user u0_ WHERE u0_.id = 127':
SQLSTATE[42S22]: Column not found: 1054 Champ 'u0_.username' inconnu dans field list
:-)
试试这个!
$users = $this->getDoctrine()->getRepository("UserBundle:User")->findBy(array("email" => $email));
或者只有一个:
$user = $this->getDoctrine()->getRepository("UserBundle:User")->findOneBy(array("email" => $email));
祝你好运! :-)
Symfony EntityRepository
创建 queryBuilder
:
class UserRepository extends EntityRepository
{
public function findEmailDQL($email)
{
return $this->createQueryBuilder('u')
->select('u.username')
->where('u.your_stuff_field = :stuff')
->setParameter('stuff' => $email)
->getQuery()
->getSingleResult();
}
}
我正在使用 symfony 框架,当我尝试使用此 DQL 方法为 FOSUser 包
生成的 Fos_user table 恢复用户名时
namespace UserBundle\Repository;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function findEmailDQL($email)
{
$query=$this->getEntityManager()
->createQuery("SELECT u.username
FROM UserBundle:User u
WHERE u.id = $email");
return $query->getResult();
}
}
具有控制器功能
public function findByEmailJsonAction($email)
{
$users=$this->getDoctrine()->getManager()
->getRepository('UserBundle:User')
->findEmailDQL($email);
$serializer = new Serializer([new ObjectNormalizer()]);
$formatted =$serializer->normalize($users);
return new JsonResponse($formatted);
}
当我尝试查找 ID 为 127 的用户时,我不断收到此错误:
执行时出现异常'SELECT u0_.username AS username_0 FROM user u0_ WHERE u0_.id = 127':
SQLSTATE[42S22]: Column not found: 1054 Champ 'u0_.username' inconnu dans field list
:-)
试试这个!
$users = $this->getDoctrine()->getRepository("UserBundle:User")->findBy(array("email" => $email));
或者只有一个:
$user = $this->getDoctrine()->getRepository("UserBundle:User")->findOneBy(array("email" => $email));
祝你好运! :-)
Symfony EntityRepository
创建 queryBuilder
:
class UserRepository extends EntityRepository
{
public function findEmailDQL($email)
{
return $this->createQueryBuilder('u')
->select('u.username')
->where('u.your_stuff_field = :stuff')
->setParameter('stuff' => $email)
->getQuery()
->getSingleResult();
}
}