在Manytomany关系中提交数据



我有2个实体,停车场和代理,每个停车位都有许多代理,每个代理商都可以管理许多停车位。

我建立了关系后,学说会自动添加一个名为"停车代理"的联接表。

现在,我正在尝试通过一种表格填充该桌子,例如创建新代理时,我可以给他一个或多个停车场,反之亦然。我尝试在表单中添加具有多种选择的选择性型,但它不起作用。

你们能帮我吗?

实体代理:

<?php
/**
 * @ORMEntity
 * @UniqueEntity(fields="username", message="Username already taken")
 */
class Agent implements UserInterface
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    public function getId()
    {
        return $this->id;
    }
    /**
     * @ORMColumn(type="string", length=191, unique=true)
     * @AssertNotBlank()
     */
    private $username;
    /**
     * @AssertLength(max=191)
     */
    private $plainPassword;
    /**
     * The below length depends on the "algorithm" you use for encoding
     * the password, but this works well with bcrypt.
     *
     * @ORMColumn(type="string", length=64)
     */
    private $password;
    /**
     * @ORMManyToMany(targetEntity="AppEntityParking", 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
/**
 * @ORMEntity(repositoryClass="AppRepositoryParkingRepository")
 */
class Parking
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;
    /**
     * @ORMColumn(type="string", length=55)
     */
    private $libelle;

    /**
     * @ORMManyToMany(targetEntity="AppEntityagent", 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 AppForm;
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:

        ->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
        ])

在此示例中,您可以看到一个带有posibillity的元素以选择多件事。

重要的是属性"多个",将其设置为"多个"或true。

相关内容

  • 没有找到相关文章

最新更新