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 ]) ; } 
  • Please remove from the question all the code that is not directly related to the case. And then two foot wrappers on over9000 lines is a bust. - Dmitriy Simushev
  • I apologize, I fixed everything. - Vladislav
  • And where is the mapping itself? It is not enough - / ** * @var $ mentors * @ORM \ ManyToMany (targetEntity = "AppBundle \ Entity \ User", inversedBy = "services") * / - Dmitry Malyshenko
  • And what else might be missing here? The connection of the tables on the machine is done, the cascade is not yet needed. Or in this case, still missing something? - Vladislav
  • cascade persist try to add, as I understand it, that the Service is added to your entity in the form? Check $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

1 answer 1

I looked at the code, you have errors there of course.

First name of the methods. If you have $mentors field, then call the addMentors() method, why addMentor ? Doctrine where it knows about? The form, I suspect guesses, but the doctrine is not a fact that it knows how.

This method is also in doubt.

 public function addMentor(\AppBundle\Entity\User $mentor) { $mentor->addService($this); } 

You must add $mentor to the $mentor collection, according to this

 public function addMentors(\AppBundle\Entity\User $mentor) { $this->mentors->add($mentor); } 

Further, the doctrine just from this field should pick up a new element of the collection and, if a cascade is indicated, make the persist automatic. If there is no cascade, in my opinion, it will give an error and report that the cascade would be worth adding.

In general, look carefully at the name of the method and what I wrote about the field.

  • Thanks for the answer. In essence, all these methods were automatically generated by the doctrine by the automaton. doctrine:generate:entities AppBundle:User . Now I will try to change the names of the methods and check. - Vladislav
  • Honestly, I don’t think that Doctrine can correctly generate methods for relationships or can it? Judging by the fact that it does not work for you, it does not know how. - zalex
  • By the way, the addMentor method shown in the example works as it should. That is, I can easily assign any number of users (mentors) to the Service. But entering the User and trying to assign Services to him, it no longer works. But now I’ll correct the names and check them out. As for the skills of the Doctrine, I can’t say anything at all, I'm a newbie for now. - Vladislav
  • No, listen, the addMentor method in your case adds $this to $mentor , i.e. service , you need to add $mentor to the $mentors - zalex collection
  • That's right, I apparently deleted the addition by accident. But still nothing works, after returning. Maybe I will update the cap? to see how the code looks now? Seriously, I don’t know what to complain about ... - Vladislav