问题
I have 2 entities, Parking and Agent, each parking can have many Agents and each Agent can administer many parkings.
After I created the relationship, Doctrine automatically added a join table called parking-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.
Can you guys help me ?
Entity 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;
}
}
Entity Parking:
<?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;
}
}
My Form:
<?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:
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.
The important thing is the attribute 'multiple', set this to 'multiple' or true.
来源:https://stackoverflow.com/questions/55923233/submiting-data-in-a-manytomany-relation