以ManyToMany关系提交数据

时间:2019-04-30 14:34:31

标签: symfony doctrine-orm doctrine symfony4

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

创建关系后,Doctrine会自动添加一个称为parking-Agent的联接表。

现在,我正尝试通过表格填充该表格,例如在创建新的特工时,我可以给他一个或多个停车位,或反之。我尝试将具有多个选择的选择类型添加到表单,但是没有用。

你们能帮我吗?

实体代理:

<?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,
        ]);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在一个可以选择多个元素的字段中尝试使用。 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
        ])

在此示例中,您可以看到一个元素,可以选择多个对象。

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