[英]Doctrine2 FindOneBy function returns empty array
I'm using Symfony2 along with doctrine2. 我正在使用Symfony2和doctrine2。
I need to know if a username
exist on a table, so I'm calling this method by AJAX... 我需要知道表上是否存在
username
,所以我要通过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. 该方法按预期工作,如果数据库中存在
username
,则返回true;否则返回false。 But when returning the user data from an existing user ($usuario)
, it always return an empty JSON array ({}) and not the expected object. 但是,当从现有用户
($usuario)
返回用户数据时,它总是返回一个空的JSON数组({}),而不是预期的对象。 Any ideas? 有任何想法吗?
If I var_dump($usuario
) before returning the response it prints out all the correct fields and values for that username
. 如果我在返回响应之前使用
var_dump($usuario
),它将打印出该username
所有正确字段和值。
Indeed, all properties of your user are private. 实际上,您用户的所有属性都是私有的。 Yet json_encode, encode only public object properties.
但是json_encode仅编码公共对象属性。
You can so implement JsonSerializable. 您可以这样实现JsonSerializable。 see more details here or set these properties to public (worse solution)
在此处查看更多详细信息或将这些属性设置为public(更糟糕的解决方案)
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.