0
被檢測到循環引用i執行此操作步驟:誤差對symfony的3
1)我打電話給我的API端點:http://localhost:8000/api/addrole
2.)這是一個稱爲控制器:
/**
* @Rest\Post("/addrole")
*/
public function addRoleAction(Request $request)
{
$userid = $request->get('userid');
$assignedRole = $request->get('role');
$assignedRoleName = $request->get('role_name');
// Obtain the User
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')
->find($userid);
// If the user gives not exists, throw error
if (!$user) {
throw new HttpException (400,"No se ha encontrado el usuario solicitado: " .$userid);
}
// obtain present user roles
$presentRoles = $user->getRoles();
$role_length = count($presentRoles);
$role_list = array();
for ($i=0; $i <$role_length ; $i++) {
array_push($role_list,$presentRoles[$i]->getRole());
}
if(!in_array($assignedRole,$role_list)){
$role = $em->getRepository('AppBundle:Role')
->findOneBy(array('role' => $assignedRole));
$user->addRole($role);
$em->persist($user); // persisting only the user.
$em->flush();
$data = array(
'result' => 'Rol asignado',
'user' => $user,
'assignedRole' => $assignedRole
);
return $data;
} else {
throw new HttpException (400,"El usuario ya posee el rol solicitado");
}
}
3.)在此步驟中的數據:
$user->addRole($role);
$em->persist($user); // persisting only the user.
$em->flush();
被保存在DB中的數據。
4),這是響應:
{
"error": {
"code": 500,
"message": "Internal Server Error",
"exception": [
{
"message": "A circular reference has been detected (configured limit: 1).",
"class": "Symfony\\Component\\Serializer\\Exception\\CircularReferenceException",...
5)如果我評論這一行:
//$em->flush();
數據obviosly不保存,但不扔的錯誤。
有關這方面的任何線索?
你有這個實體的任何事件監聽器嗎?或者你在Repository中有一些依賴關係? (注入) – Vamsi
上帝......我發現了錯誤@Vamsi錯誤是代碼後''em-> flush();'this:'$ data = array( 'result'=>'Rol asignado', 'user'=> $ user, 'assignedRole'=> $ assignedRole ); 返回$ data;'我會盡快更新我的答案。謝謝你的幫助。 –