【问题标题】:Symfony - ManyToMany - replaces instead of addingSymfony - ManyToMany - 替换而不是添加
【发布时间】:2020-11-25 08:51:21
【问题描述】:

如果我的问题对你来说很愚蠢,我提前道歉,我是初学者,我已经搜索过但找不到答案。

我们在工厂里。在这个工厂里,每个工人可以有几个职位,每个职位可以有几个工人。所以我们处于ManyToMany关系。问题是当我在帖子中添加一个工人时,他并没有添加到这个帖子中已经存在的工人,而是替换了他!好像一个帖子只能包含一个工人。 有人可以告诉我我做错了什么或将与此类问题相关的文档准确地发送给我吗? 谢谢。 这是相关代码。

(Poste = Post,Operateur = 工人)

在帖子实体中:

     /**
     * @ORM\ManyToMany(targetEntity=Operateur::class, inversedBy="postes")
     * @ORM\JoinTable(name="poste_operateur")
     */
    private $operateurs;

     /**
     * @return Collection|Operateur[]
     */
    public function getOperateurs(): Collection
    {
        return $this->operateurs;
    }

    public function addOperateur(Operateur $operateur): self
    {
        if (!$this->operateurs->contains($operateur)) {
            $this->operateurs[] = $operateur;
            $operateur->addPoste($this);
        }

        return $this;
    }

    public function removeOperateur(Operateur $operateur): self
    {
        $this->operateurs->removeElement($operateur);
        $operateur->removePoste($this);

        return $this;
    }

在操作员(工人)实体中:

/**
 * @ORM\ManyToMany(targetEntity=Poste::class, mappedBy="operateurs")
 */
private $postes;


/**
* @return Collection|Poste[]
*/
public function getPostes(): Collection
{
    return $this->postes;
}

public function addPoste(Poste $poste): self
{
    if (!$this->postes->contains($poste)) {
        $this->postes[] = $poste;
        $poste->addOperateur($this);
    }

    return $this;
}

public function removePoste(Poste $poste): self
{
    if ($this->postes->removeElement($poste)) {
        $poste->removeOperateur($this);
    }

    return $this;
}

在 PosteController 中,将操作符添加到帖子的方法:

/**
 * @Route("/{id}/new", name="poste_ope", methods={"GET", "POST"})
 */
public function addOpe(Request $request, Poste $poste): Response
{
    $form = $this->createForm(PosteType2::class, $poste);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $this->getDoctrine()->getManager()->flush();

        $this->addFlash(
            'success',
            "L'opérateur a bien été ajouté au poste {$poste->getNom()}  !"
        );
        return $this->redirectToRoute('operateur_index');
    }

    return $this->render('poste/addope.html.twig', [
        'poste' => $poste,
        'form' => $form->createView(),
    ]);
}

PostType2 中的表单:

class PosteType2 extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('operateurs', EntityType::class, [
                'class' => Operateur::class, 
                'label' => 'ajouter un opérateur à ce poste',
                'choice_label' => 'nom',
                'multiple' => true,
                'expanded' => true,
            ])
            ->add('save', SubmitType::class, [
                'label' => 'Enregistrer',
                'attr' => [
                    'class' => 'btn btn-primary'
                ]
            ]);
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Poste::class,
        ]);
    }
}

【问题讨论】:

  • 您可以尝试将$this->postes[] = $poste; 替换为$this->postes->add($poste); 吗?
  • 每个类中是否有构造函数,在 Post 中使用 $this->operateurs = new ArrayCollection(),在 Operateurs 中使用 $this->postes = new ArrayCollection()?

标签: symfony doctrine-orm many-to-many symfony5


【解决方案1】:

问题出在 PosteController 中,这里是更正:

add an addPost

这是帮助我的文档:https://symfony.com/doc/current/doctrine/associations.html#saving-related-entities

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    相关资源
    最近更新 更多