Symfony FormType Error

I have a mistake when I create a provider.

Entities passed to the choice field must be managed. Maybe persist them in the entity manager?

My formType PrestataireType

/** * @var bool $admin */
private $admin;
/** * @var User $user */
private $user;
/** * DemandeType constructor. * @param bool|false $admin */
public function __construct($admin = false, $user, $badges = null)
{ $this->admin = $admin; $this->user = $user;
}
/** * {@inheritdoc} */
public function buildForm(FormBuilderInterface $builder, array $options)
{ $builder ->add('files', FileType::class, array( 'required' => false, 'attr' => array('class' => 'hidden') ) ) ->add('name') ->add('address') ->add('city') ->add('zipcode') ->add('phone') ->add('email') ->add('dateStartContract') ->add('dateEndContract') ->add('frequency') ->add('intervention', CollectionType::class, array( 'entry_type' => EntityType::class, 'entry_options' => array( 'class' => 'AppBundle:Intervention', 'expanded' => false, 'choice_label' => 'id' ), 'allow_add' => true, 'allow_delete' => true, 'invalid_message' => "Un destinataire n'existe pas", )); if ($this->user->getResidence() != $this->admin) { $builder->add('residence', EntityType::class, array( 'class' => 'AppBundle:Residence', 'choices' => $this->user->getResidence(), )); } else { $builder->add('residence', 'entity', array( 'class' => 'AppBundle:Residence', 'choice_label' => 'name', )); } ;
}

(Entities are objects with identity. Their identity has a conceptual meaning inside your domain. In a CMS application each article has a unique id. You can uniquely identify each article by that id.)My entity Prestataire.php

 /** * Prestataire * * @ORM\Table(name="prestataire") * @ORM\Entity(repositoryClass="AppBundle\Repository\PrestataireRepository") */
class Prestataire
{ /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="name", type="string", length=255) */ private $name; /** * @var string * * @ORM\Column(name="address", type="string", length=255) */ private $address; /** * @var string * * @ORM\Column(name="city", type="string", length=255) */ private $city; /** * @var string * * @ORM\Column(name="zipcode", type="string", length=255) */ private $zipcode; /** * @var string * * @ORM\Column(name="phone", type="string", length=255) */ private $phone; /** * @var string * * @ORM\Column(name="email", type="string", length=255) */ private $email; /** * @var \DateTime * * @ORM\Column(name="date_start_conttract", type="date") */ private $dateStartContract; /** * @var \DateTime * * @ORM\Column(name="date_end_contract", type="date") */ private $dateEndContract; /** * @var string * * @ORM\Column(name="frequency", type="string", length=255) */ private $frequency; /** * @var \DateTime * * @ORM\Column(name="date_create", type="date") */ private $dateCreate; /** * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Residence", inversedBy="prestataire") * @ORM\JoinTable(name="prestataire_resid", * joinColumns={@ORM\JoinColumn(name="prestataire_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="residence_id", referencedColumnName="id")} * ) */ private $residence; /** * Temp file * @var File $file */ public $files; /** * @var * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Upload", cascade={"remove", "persist"}) */ private $uploads; /** * @ORM\OneToMany(targetEntity="AppBundle\Entity\Intervention", mappedBy="prestataire", cascade={"persist","remove"}) * @ORM\JoinColumn(nullable=true) */ private $intervention; /** * Prestataire constructor. */ public function __construct() { $this->residence = new ArrayCollection(); $this->uploads = new ArrayCollection(); $this->dateCreate = new \DateTime(); $this->updatedAt = clone $this->dateCreate; $this->intervention = new ArrayCollection(); } function getResidence(){ return $this->residence; } /** * Add residence * * @param \AppBundle\Entity\Residence $residence * * @return Prestataire */ public function addResidence(\AppBundle\Entity\Residence $residence) { $this->residence[] = $residence; return $this; } /** * Remove residence * * @param \AppBundle\Entity\Residence $residence */ public function removeResidence(\AppBundle\Entity\Residence $residence) { $this->residence->removeElement($residence); } /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Set name * * @param string $name * * @return Prestataire */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set address * * @param string $address * * @return Prestataire */ public function setAddress($address) { $this->address = $address; return $this; } /** * Get address * * @return string */ public function getAddress() { return $this->address; } /** * Set city * * @param string $city * * @return Prestataire */ public function setCity($city) { $this->city = $city; return $this; } /** * Get city * * @return string */ public function getCity() { return $this->city; } /** * Set zipcode * * @param string $zipcode * * @return Prestataire */ public function setZipcode($zipcode) { $this->zipcode = $zipcode; return $this; } /** * Get zipcode * * @return string */ public function getZipcode() { return $this->zipcode; } /** * Set phone * * @param string $phone * * @return Prestataire */ public function setPhone($phone) { $this->phone = $phone; return $this; } /** * Get phone * * @return string */ public function getPhone() { return $this->phone; } /** * Set email * * @param string $email * * @return Prestataire */ public function setEmail($email) { $this->email = $email; return $this; } /** * Get email * * @return string */ public function getEmail() { return $this->email; } /** * Set dateStartContract * * @param \DateTime $dateStartContract * * @return Prestataire */ public function setDateStartContract($dateStartContract) { $this->dateStartContract = $dateStartContract; return $this; } /** * Get dateStartContract * * @return \DateTime */ public function getDateStartContract() { return $this->dateStartContract; } /** * Set dateEndContract * * @param \DateTime $dateEndContract * * @return Prestataire */ public function setDateEndContract($dateEndContract) { $this->dateEndContract = $dateEndContract; return $this; } /** * Get dateEndContract * * @return \DateTime */ public function getDateEndContract() { return $this->dateEndContract; } /** * Set frequency * * @param string $frequency * * @return Prestataire */ public function setFrequency($frequency) { $this->frequency = $frequency; return $this; } /** * Get frequency * * @return string */ public function getFrequency() { return $this->frequency; } /** * Set dateCreate * * @param \DateTime $dateCreate * * @return Prestataire */ public function setDateCreate($dateCreate) { $this->dateCreate = $dateCreate; return $this; } /** * Get dateCreate * * @return \DateTime */ public function getDateCreate() { return $this->dateCreate; } /** * Add upload * * @param \AppBundle\Entity\Upload $upload * * @return Prestataire */ public function addUpload(\AppBundle\Entity\Upload $upload) { $this->uploads[] = $upload; return $this; } /** * Remove upload * * @param \AppBundle\Entity\Upload $upload */ public function removeUpload(\AppBundle\Entity\Upload $upload) { $this->uploads->removeElement($upload); } /** * Get uploads * * @return \Doctrine\Common\Collections\Collection */ public function getUploads() { return $this->uploads; } /** * Set updatedAt * * @param \DateTime $updatedAt * * @return Prestataire */ public function setUpdatedAt($updatedAt) { $this->updatedAt = $updatedAt; return $this; } /** * Get updatedAt * * @return \DateTime */ public function getUpdatedAt() { return $this->updatedAt; } /** * Add intervention * * @param \AppBundle\Entity\Intervention $intervention * * @return Prestataire */ public function addIntervention(\AppBundle\Entity\Intervention $intervention) { $this->intervention[] = $intervention; return $this; } /** * Remove intervention * * @param \AppBundle\Entity\Intervention $interventions */ public function removeIntervention(\AppBundle\Entity\Intervention $interventions) { $this->intervention->removeElement($interventions); } /** * Get intervention * * @return \Doctrine\Common\Collections\Collection */ public function getIntervention() { return $this->intervention; }
}

Thank you for help.

1 Answer

I think you need to set your field $intervention to be plural in your entity:

private $interventions;

Then you'll have to match this in your FormType:

// ...
->add('interventions', CollectionType::class, array(
// ...

and in your add/remove methods:

public function addIntervention(\AppBundle\Entity\Intervention $intervention)
{ $this->interventions[] = $intervention; return $this;
}
public function removeIntervention(\AppBundle\Entity\Intervention $interventions)
{ $this->interventions->removeElement($interventions);
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like