创建提供商时出错。
必须管理传递到选择字段的实体。也许坚持他们在实体经理?
我的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',
));
}
;
}
(实体是具有身份的对象。他们的身份在您的域内具有概念意义。在CMS应用程序中,每篇文章都有唯一的ID。您可以通过该ID唯一地标识每篇文章。) 我的实体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;
}
}
感谢您的帮助。
答案 0 :(得分:0)
我认为您需要在您的实体中将您的字段$干预设置为复数:
private $interventions;
然后你必须在你的FormType中匹配它:
// ...
->add('interventions', CollectionType::class, array(
// ...
并在您的添加/删除方法中:
public function addIntervention(\AppBundle\Entity\Intervention $intervention)
{
$this->interventions[] = $intervention;
return $this;
}
public function removeIntervention(\AppBundle\Entity\Intervention $interventions)
{
$this->interventions->removeElement($interventions);
}