In my symfony 2.7 application, I have a custom object based on FOSUserBundle . The user must be connected to access the addAction () form.
My custom object is associated with another object called parc. This is the code of my two objects.
Users.php
class Users extends BaseUser { protected $id; public function __construct() { parent::__construct(); $this->parcs = new \Doctrine\Common\Collections\ArrayCollection(); } private $parcs; public function getParcs() { return $this->parcs; } public function setParcs($parcs) { return $this->parcs; }
Parcs.php
class Parcs { /** * @var integer * * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var \Doctrine\Common\Collections\Collection * * @ORM\ManyToMany(targetEntity="Users", mappedBy="parcs") */ private $users; public function __construct() { $this->users = new \Doctrine\Common\Collections\ArrayCollection(); } //rest of my entity attributes, object, properties etc // getters and setters /** * Get users * * @return \Doctrine\Common\Collections\Collection */ public function getUsers() { return $this->users; } /** * Set users * * @return \Doctrine\Common\Collections\Collection */ public function setUsers($users) { return $this->users; } /** * Add user * * @param \MySpace\MyBundle\Entity\Users $user * * @return Parcs */ public function addUser(\MySpace\MyBundle\Entity\Users $user) { $this->users[] = $user; return $this; }
Using FOSUserBundle to access the connected user, in my twig view I do this: {{ app.user.username }} , which returns me the username of the current user.
Now I would like to get this value in my symfony form (for user field) when I would like to add parc. This is my form of parcsType.php :
public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('parcName') ->add('users') ; }
This is my form in twig view:
<form action="{{ path('addParcs_process') }}" method="POST" {{ form_enctype(form) }}> <div> {{ form_label(form.parcName, "Enter the parc name: ", {'label_attr': {'class': 'control-label'}}) }} {{ form_errors(form.parcName) }} {{ form_widget(form.parcName) }} </div> <div> {{ form_label(form.users, "Vous ajoutez ce parc en tant que: ", {'label_attr': {'class': 'control-label'}}) }} {{ form_errors(form.users) }} {{ form_widget(form.users, {'attr': {'value': app.user.username }}) }} </div> <input type="submit" value="Ajouter" class="btn btn-success"/> </form>
As you can see, I set the default value for my user field {{ app.user.username }} and it works, my user field returns me the current user. But when I submit the form, the user field and the association ( ManyToMany ) between Parcs.php and Users.php not saved / not cleared.
This is my controller code:
public function addParcsAction() { if (!$this->get('security.context')->isGranted('ROLE_EDIT')) { throw new HttpException("403"); } $em=$this->getDoctrine()->getManager(); $parc = new Parcs; $form=$this->createForm(new ParcsType(), $parc); $form->add('user'); $request = $this->getRequest(); if ($request->isMethod('POST') | ($form->isValid())) { $form->bind($request); $currentUser=$this->container->get('security.context')->getToken()->getUser(); $parc->setUsers($currentUser); $parc = $form->getData(); $em->persist($parc); $em->flush(); return $this->redirect($this->generateUrl('indexParc')); } else { return $this->render('MySpaceMyBundle:Parcs:addParcs.html.twig', array('form' => $form->createView() )); } }
The view works well when I enter a value for the name parc, the data is logged in my database, but not the user value and the ManyToMany association.