I am using symfony framework, and when I tried this DQL methode to recover username for the Fos_user table generated by the FOSUser bundle
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();
}
}
with the controller function
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);
}
I keep getting this error when I try looking for the user with id 127:
An exception occurred while executing '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
:-)
Try this !
$users = $this->getDoctrine()->getRepository("UserBundle:User")->findBy(array("email" => $email));
Or for just one:
$user = $this->getDoctrine()->getRepository("UserBundle:User")->findOneBy(array("email" => $email));
Good luck ! :-)