symfony - Symfony2 add current user to form input data -


i've got standard fos user that's got manytoone entity relationship blog entity.

/**  * @orm\manytoone(targetentity="user", inversedby="blog")  * @orm\joincolumn(name="user", referencedcolumnname="id")  */ private $user; 

i've used app/console generate:doctrine:crud command generate basic crud system.

when create new blog entity want insert logged in user performed action. default symfony generates crud code allows user select user connected entity.

    $builder         ->add('name')         ->add('address')         ->add('facebook')         ->add('twitter')         ->add('user')     ; 

in controller can access logged in user using security.context:

$user = $this->get('security.context')->gettoken()->getuser(); 

whats best course of action change this?

okay! solved own question (if i'm doing wrong please feel free correct me, , i'll accept better answer). 1 appear work however!

the user can added in controller after $form->isvalid() check performed. here's controller function example.

public function createaction(request $request) {     $entity  = new blog();     $form = $this->createform(new blogtype(), $entity);     $form->bind($request);      if ($form->isvalid()) {         $entity->setuser($this->get('security.context')->gettoken()->getuser());         $em = $this->getdoctrine()->getmanager();         $em->persist($entity);         $em->flush();         return $this->redirect($this->generateurl('blog_show', array('id' => $entity->getid())));     }      return $this->render('examplebundle:blog:new.html.twig', array(         'entity' => $entity,         'form'   => $form->createview(),     )); } 

the user needs removed form builder work.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -