以ManyToMany关系提交数据 [英] Submiting data in a ManyToMany relation

查看:116
本文介绍了以ManyToMany关系提交数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体,Parking和Agent,每个停车场可以有多个Agent,每个Agent可以管理多个停车场。

I have 2 entities, Parking and Agent, each parking can have many Agents and each Agent can administer many parkings.

创建关系后,Doctrine自动添加

After I created the relationship, Doctrine automatically added a join table called parking-Agent.

现在,我正在尝试通过表单填充该表,例如在创建新的Agent时,我可以给他一个或多个停车位, 或相反亦然。我尝试在表单中添加具有多个选择的选择类型,但没有用。

Now I'm trying to populate that table through a form, like when creating a new Agent I can give him one or many parkings, or Vice-Versa. I tried adding a choicetype with multiple choices to the form but it didn't work.

你们能帮我吗?

实体代理:

<?php

/**
 * @ORM\Entity
 * @UniqueEntity(fields="username", message="Username already taken")
 */
class Agent implements UserInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    public function getId()
    {
        return $this->id;
    }
    /**
     * @ORM\Column(type="string", length=191, unique=true)
     * @Assert\NotBlank()
     */
    private $username;

    /**
     * @Assert\Length(max=191)
     */
    private $plainPassword;
    /**
     * The below length depends on the "algorithm" you use for encoding
     * the password, but this works well with bcrypt.
     *
     * @ORM\Column(type="string", length=64)
     */
    private $password;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Parking", mappedBy="agents")
     */
    private $parkings;
    public function __construct()
    {
        $this->parkings = new ArrayCollection();
    }
    public function getUsername()
    {
        return $this->username;
    }

    public function setUsername($username)
    {
        $this->username = $username;
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }
    public function setPlainPassword($password)
    {

        $this->plainPassword = $password;
        $this->password = null;

    }
    public function getPassword()
    {
        return $this->password;
    }
    public function setPassword($password)
    {
        if (!is_null($password)) {
        $this->password = $password;
        }
            return $this;

    }

    public function getSalt()
    {
        return null;
    }

    public function eraseCredentials()
    {
    }

    /**
     * @return Collection|Parking[]
     */
    public function getParkings(): Collection
    {
        return $this->parkings;
    }

    public function addParking(Parking $parking): self
    {
        if (!$this->parkings->contains($parking)) {
            $this->parkings[] = $parking;
            $parking->addAgent($this);
            return $this;
        }

        return $this;
    }

    public function removeParking(Parking $parking): self
    {
        if ($this->parkings->contains($parking)) {
            $this->parkings->removeElement($parking);
            $parking->removeAgent($this);
        }

        return $this;
    }
}

实体停车:

<?php

/**
 * @ORM\Entity(repositoryClass="App\Repository\ParkingRepository")
 */
class Parking
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=55)
     */
    private $libelle;


    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\agent", inversedBy="parkings")
     */
    private $agents;


    public function __construct()
    {
        $this->agents = new ArrayCollection();
        $this->voitures = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getLibelle(): ?string
    {
        return $this->libelle;
    }

    public function setLibelle(string $libelle): self
    {
        $this->libelle = $libelle;

        return $this;
    }


    /**
     * @return Collection|agent[]
     */
    public function getAgents(): Collection
    {
        return $this->agents;
    }

    public function addAgent(Agent $agent): self
    {
        if (!$this->agents->contains($agent)) {
            $this->agents[] = $agent;
        }

        return $this;
    }

    public function removeAgent(Agent $agent): self
    {
        if ($this->agents->contains($agent)) {
            $this->agents->removeElement($agent);
        }

        return $this;
    }


}

我的表单:

<?php
namespace App\Form;

ass ParkingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('libelle');
    }

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


推荐答案

您可以在一个可以选择多个元素的字段中尝试使用。 Select2:

You can try it with a field where you can select multiple elements. The Select2:

        ->add('personsconcerned', ChoiceType::class, [
            'label' => 'form.personsconcerned',
            'choices' => $this->groupService->getMailGroups(),
            'multiple' => 'multiple',
            'mapped' => false,
            'choice_translation_domain' => false,
            'attr' => [
                'data-select' => 'true'
            ],
            'data' => $mailgroups
        ])

例如,您可以看到一个元素可以选择多个对象。

In this example you can see an Element with the posibillity to select more than one thing.

重要的是属性 multiple,将其设置为 multiple或true。

The important thing is the attribute 'multiple', set this to 'multiple' or true.

这篇关于以ManyToMany关系提交数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆