我执行以下步骤:
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();
正在将数据保存在数据库中。
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();
数据显然没有保存,但错误并没有丢失。
关于此的任何线索?
答案 0 :(得分:0)
最后我发现了错误。
问题在于此代码:
...
post $em->flush();
this: $data = array('result' => 'Rol asignado', 'user' => $user, 'assignedRole' => $assignedRole );
return $data;
在刷新之后我调用了$ user对象。这就产生了问题。
感谢所有人。