Good day. I work in connection with ManyToMany.
There are two objects: Service and User.
class Service { ... /** * @var $mentors * @ORM\ManyToMany(targetEntity="AppBundle\Entity\User", inversedBy="services", cascade={"persist"}) */ private $mentors; /** * Constructor */ public function __construct() { $this->mentors = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add mentors * @param \AppBundle\Entity\User $mentor * @return Service */ public function addMentors(\AppBundle\Entity\User $mentor) { $this->mentors->add($mentor); } /** * Remove mentor * @param \AppBundle\Entity\User $mentor */ public function removeMentors(\AppBundle\Entity\User $mentor) { $this->mentors->removeElement($mentor); } /** * Get mentors * @return \Doctrine\Common\Collections\ArrayCollection */ public function getMentors() { return $this->mentors; } } And User:
class User extends BaseUser { ... /** * @var \Doctrine\Common\Collections\ArrayCollection * @ORM\ManyToMany(targetEntity="App\AdminBundle\Entity\Service", mappedBy="mentors", cascade={"persist"}) */ protected $services; public function __construct() { $this->services = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add services * @param \App\AdminBundle\Entity\Service $service * @return User */ public function addServices(\App\AdminBundle\Entity\Service $service) { $this->services->add($service); $service->addMentors($this); } /** * Remove service * @param \App\AdminBundle\Entity\Service $service */ public function removeServices(\App\AdminBundle\Entity\Service $service) { $this->services->removeElement($service); } /** * Get services * @return \Doctrine\Common\Collections\Collection */ public function getServices() { return $this->services; } } Feedback for the class User did according to the instructions. If I make changes from the Service side, everything works fine. But if I try to add a service on the part of the Participants, it no longer works, although there is a feedback:
public function addService(\App\AdminBundle\Entity\Service $service) { $service->addMentor($this); $this->services[] = $service; } Content UserController.php
public function editAction(Request $request, User $user) { $editForm = $this->createForm('AppBundle\Form\UserType', $user); $editForm->handleRequest($request); if ($editForm->isSubmitted() && $editForm->isValid()) { $this->getDoctrine()->getManager()->flush(); return $this->redirectToRoute('user_edit', array('id' => $user->getId())); } return $this->render('user/edit.html.twig', array( 'user' => $user, 'edit_form' => $editForm->createView() )); } What should the entry look like in the controller? I have already tried everything possible, changes in user data are happening, but services are not assigned.
Usertype
public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('services',null,[ 'label' => 'Przypisane usługi', 'expanded' => true, 'multiple' => true ]) ; }
$service = $form->getData()->getService()after processing the form, is there an entity there? Well, you can get straight with nails -$this->getDoctrine()->getManager()->persist($service);before flush (), since nothing helps. - zalex