douhuan1950 2018-08-08 15:35
浏览 141
已采纳

OneToMany不保存更改

I have a oneToMany - ManyToOne relation between two entities.

When editing on the Departement edit page (owning side, ManyToOne), changes will be saved into the departement table,
but editing from the Utilisateur edit page (inverse side, OneToMany), changes won't be saved into the Departement table.

Can someone exmplain me why it's not working?

<kbd>src/AppBundle/Entity/Utilisateur.php</kbd>

class Utilisateur implements UserInterface, \Serializable {
    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Departement", mappedBy="commercial")
     */
    private $departements;

    /**
     * Constructor
     */
    public function __construct() {
        $this->departements=new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * @param \AppBundle\Entity\Departement $departement
     * @return Utilisateur
     */
    public function addDepartement(\AppBundle\Entity\Departement $departement)
    {
        $this->departements[] = $departement;

        return $this;
    }

    /**
     * @param \AppBundle\Entity\Departement $departement
     */
    public function removeDepartement(\AppBundle\Entity\Departement $departement)
    {
        $this->departements->removeElement($departement);
    }

    /**
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getDepartements()
    {
        return $this->departements;
    }
}

<kbd>src/AppBundle/Entity/Departement.php</kbd>

class Departement {
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Utilisateur", inversedBy="departements")
     */
    private $commercial;

    /**
     * @param \AppBundle\Entity\Utilisateur $commercial
     * @return Departement
     */
    public function setCommercial(\AppBundle\Entity\Utilisateur $commercial=null) {
        $this->commercial=$commercial;

        return $this;
    }

    /**
     * @return \AppBundle\Entity\Utilisateur
     */
    public function getCommercial() {
        return $this->commercial;
    }
}

<kbd>src/AppBundle/Form/Utilisateur/Edit3dhType.php</kbd>

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('departements', EntityType::class, array(
        'class'=>Departement::class,
        'expanded'=>true,
        'multiple'=>true
    ));
}

<kbd>src/AppBundle/Controller/UtilisateurController.php</kbd>

/**
 * @Route("/dashboard/utilisateurs/edit-{id}", name="security_edit_user")
 * @Method({"GET", "POST"})
 *
 * @param Request $request
 * @param Utilisateur $utilisateur
 * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
 */
public function editAction(Request $request, Utilisateur $utilisateur) {
    $logo=$utilisateur->getLogo();
    if($utilisateur->getLogo() !== null) {
        $utilisateur->setLogo(new File($this->getParameter('dir_picto').$logo));
    }

    $form=$this->createForm(Edit3dhType::class, $utilisateur, array(
        'action'=>$this->generateUrl('security_edit_user', array('id'=>$utilisateur->getId())),
        'method'=>'POST',
    ));

    $form->handleRequest($request);
    if($form->isSubmitted() && $form->isValid()) {
        if($utilisateur->getLogo() !== null) {
            $utilisateur->setLogo($this->container->get('service.upload')->uploadPicto($utilisateur->getLogo()));
        } else {
            $utilisateur->setLogo($logo);
        }

        $em=$this->getDoctrine()->getManager();
        $em->flush();
    }

    return $this->render('security/edit.html.twig', array(
        'user'=>$this->getUser(),
        'utilisateur'=>$utilisateur,
        'form'=>$form->createView(),
    ));
}
  • 写回答

4条回答 默认 最新

  • dongzhong9055 2018-08-09 16:21
    关注

    So... Thanks to TEDx who hinted me some docs, after hours of searchs and reading, tons of dumps and a small chat with a bear... I came to understand the following.

    First, Doctrine seems to only check data on the owning side of the relation. Meaning that it won't handle things if done from the inverse side.

    Second, related to the first problem, as I'm trying to do things on the inverse side, Doctrine won't set departement.commercial in the form array collection. Thus, finding no differences with the data in the table, it doesn't make any query.

    Last, Doctrine won't detect any unchecked entry in the form (so setting departement.commercial to NULL).

    Below the solution I came up with. It apply for the inverse side of OneToMany/ManyToOne relations. And with some tweak can also be used for ManyToMany relations.

    <kbd>src/AppBundle/Controller/UtilisateurController.php</kbd>

    public function editAction(Request $request, Utilisateur $utilisateur) {
        //Save pre submit data
        $preSubmitDepartement=$utilisateur->getDepartements()->toArray();
    
        $form=$this->createForm(UtilisateurType::class, $utilisateur, array(
            'action'=>$this->generateUrl('security_edit_user', array('id'=>$utilisateur->getId())),
            'method'=>'POST',
            'utilisateur'=>$utilisateur,
        ));
    
        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()) {
            $em=$this->getDoctrine()->getManager();
    
            //Get post submit data
            $postSubmitDepartement=$utilisateur->getDepartements()->toArray();
    
            //Keep only IDs for pre and post submit data
            /** @var Departement $v */
            foreach($preSubmitDepartement as $k=>$v) {
                $preSubmitDepartement[$k]=$v->getId();
            }
            /** @var Departement $v */
            foreach($postSubmitDepartement as $k=>$v) {
                $postSubmitDepartement[$k]=$v->getId();
            }
    
            //Find removed IDs
            $prePostSubmitDifference=array_map('unserialize', array_diff(array_map('serialize',$preSubmitDepartement), array_map('serialize',$postSubmitDepartement)));
    
            //Fetch related Departement entries
            $departements=$em->getRepository(Departement::class)->findBy(array('id'=>array_merge($postSubmitDepartement, $prePostSubmitDifference)));
    
            //setCommercial to $utilisateur or null
            /** @var Departement $departement */
            foreach($departements as $departement) {
                if(in_array($departement->getId(), $postSubmitDepartement)) {
                    $departement->setCommercial($utilisateur);
                } else if(in_array($departement->getId(), $prePostSubmitDifference)) {
                    $departement->setCommercial(null);
                }
            }
    
            $em->flush();
        }
    
        return $this->render('security/edit.html.twig', array(
            'user'=>$user,
            'utilisateur'=>$utilisateur,
            'form'=>$form->createView(),
        ));
    }
    

    Now I can set departement.conseiller from the inverse side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?