[英]Doctrine2 FindOneBy function returns empty array
我正在使用Symfony2和doctrine2。
我需要知道表上是否存在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;
}
該方法按預期工作,如果數據庫中存在username
,則返回true;否則返回false。 但是,當從現有用戶($usuario)
返回用戶數據時,它總是返回一個空的JSON數組({}),而不是預期的對象。 有任何想法嗎?
如果我在返回響應之前使用var_dump($usuario
),它將打印出該username
所有正確字段和值。
實際上,您用戶的所有屬性都是私有的。 但是json_encode僅編碼公共對象屬性。
您可以這樣實現JsonSerializable。 在此處查看更多詳細信息或將這些屬性設置為public(更糟糕的解決方案)
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.