CoreBundle/Controller/RoleController.php
public function addAction(Request $request, $id) 
    {
        $em = $this->getDoctrine()->getManager();
        $actor = $em->getRepository($this->get('core_manager')->getActorBundleName().':Actor')->findOneById($id);
        
        $trans = $this->get('translator');
        $form = $this->container->get('form.factory')->create('CoreBundle\Form\ActorRoleType');
        $form->handleRequest($request);
           
        if ($form->isSubmitted()) {
            $em = $this->getDoctrine()->getManager();
            $data = $request->request->get($form->getName());
            $role = $em->getRepository('CoreBundle:Role')->find($data['roles']);
            $actor->addRole($role);
            try {
                $em->persist($actor);
                $em->flush($actor);
                return new JsonResponse(array('status' => 'success', 'id'=> $actor->getId(), 'message'=> $trans->trans('role.created')));
            } catch (\Exception $exc) {
                return new JsonResponse(array('status' => 'error', 'message'=> $trans->trans('role.duplicate')));
            }
        }
        return new JsonResponse(array('status' => 'error', 'message'=> $trans->trans('role.form.notsubmit')));
    }
public function addAction(Request $request, $id) 
    {
        $em = $this->getDoctrine()->getManager();
        $actor = $em->getRepository($this->get('core_manager')->getActorBundleName().':Actor')->findOneById($id);
        
        $trans = $this->get('translator');
        $form = $this->container->get('form.factory')->create('CoreBundle\Form\ActorRoleType');
        $form->handleRequest($request);
           
        if ($form->isSubmitted()) {
            $em = $this->getDoctrine()->getManager();
            $data = $request->request->get($form->getName());
            $role = $em->getRepository('CoreBundle:Role')->find($data['roles']);
            $actor->addRole($role);
            try {
                $em->persist($actor);
                $em->flush($actor);
                return new JsonResponse(array('status' => 'success', 'id'=> $actor->getId(), 'message'=> $trans->trans('role.created')));
            } catch (\Exception $exc) {
                return new JsonResponse(array('status' => 'error', 'message'=> $trans->trans('role.duplicate')));
            }
        }
        return new JsonResponse(array('status' => 'error', 'message'=> $trans->trans('role.form.notsubmit')));
    }
public function addAction ( Request $ request , $ id ) { $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; $ actor = $ em -> getRepository ( $ this -> get ( 'core_manager' ) -> getActorBundleName ( ) . ':Actor' ) -> findOneById ( $ id ) ; $ trans = $ this -> get ( 'translator' ) ; $ form = $ this -> container -> get ( 'form.factory' ) -> create ( 'CoreBundle\Form\ActorRoleType' ) ; $ form -> handleRequest ( $ request ) ; if ( $ form -> isSubmitted ( ) ) { $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; $ data = $ request -> request -> get ( $ form -> getName ( ) ) ; $ role = $ em -> getRepository ( 'CoreBundle:Role' ) -> find ( $ data [ 'roles' ] ) ; $ actor -> addRole ( $ role ) ; try { $ em -> persist ( $ actor ) ; $ em -> flush ( $ actor ) ; return new JsonResponse ( array ( 'status' => 'success' , 'id' => $ actor -> getId ( ) , 'message' => $ trans -> trans ( 'role.created' ) ) ) ; } catch ( \ Exception $ exc ) { return new JsonResponse ( array ( 'status' => 'error' , 'message' => $ trans -> trans ( 'role.duplicate' ) ) ) ; } } return new JsonResponse ( array ( 'status' => 'error' , 'message' => $ trans -> trans ( 'role.form.notsubmit' ) ) ) ; }
Add role by ajax

@Route("/{id}/role/add")
@Method("POST")
Add role by ajax