I'm using Symfony2 along with doctrine2.
I need to know if a username
exist on a table, so I'm calling this method by AJAX...
public function existeUsername()
{
$req = $this->getRequest();
$user = $req->request->get('user');
$em = $this->getDoctrine()->getEntityManager();
$usuario = $em->getRepository('RECURSIVAUserBundle:Usuario')->findOneByUsername($user);
if ($usuario):
//user found
$response = new Response(json_encode(array('error' => true, 'usuario' => $usuario, 'user' => $user)));
$response->headers->set('Content-Type', 'application/json');
return $response;
else:
//did not found the user
$response = new Response(json_encode(array('error' => false, 'user' => $user)));
$response->headers->set('Content-Type', 'application/json');
return $response;
endif;
}
The method works as expected returning true if the username
exists in the database or false if not. But when returning the user data from an existing user ($usuario)
, it always return an empty JSON array ({}) and not the expected object. Any ideas?
If I var_dump($usuario
) before returning the response it prints out all the correct fields and values for that username
.