我正在使用FOSRestBundle构建API,我正在尝试将现有的Employee
实体添加到现有的Report
实体中。但我的表单验证不断抛出错误:This form should not contain extra fields
将'allow_add' => true
添加到ReportType选项不是一个选项,因为我只想将激动的员工添加到报表中。我怎样才能做到这一点?
PUT请求正文
{
"report":{
"notes":"This is a note.",
"client":5,
"contactPerson":4,
"contributors":[
{
"id": 10
}
]
}
}
REPORTTYPE
class ReportType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('notes')
->add('client', 'entity', array(
'class' => 'ApiBundle:Client'
))
->add('contactPerson', 'entity', array(
'class' => 'ApiBundle:ContactPerson'
))
->add('contributors', CollectionType::class, array(
'entry_type' => EmployeeType::class,
'by_reference' => false
))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ApiBundle\Entity\Report',
'csrf_protection' => false,
'allow_extra_fields' => true
));
}
}
来自报告实体的代码段
/**
* @ORM\ManyToMany(targetEntity="ApiBundle\Entity\Employee", inversedBy="contributions", cascade={"persist"})
* @ORM\JoinTable(name="reports_contributors")
*/
private $contributors;
/**
* Constructor
*/
public function __construct()
{
$this->contributors = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add contributor
*
* @param \ApiBundle\Entity\Employee $contributor
*
* @return Report
*/
public function addContributor(\ApiBundle\Entity\Employee $contributor)
{
$this->contributors[] = $contributor;
return $this;
}
/**
* Remove contributor
*
* @param \ApiBundle\Entity\Employee $contributor
*/
public function removeContributor(\ApiBundle\Entity\Employee $contributor)
{
$this->contributors->removeElement($contributor);
}
/**
* Get contributors
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContributors()
{
return $this->contributors;
}
来自员工实体的代码段
/**
* @ORM\ManyToMany(targetEntity="ApiBundle\Entity\Report", mappedBy="contributors")
*/
private $contributions;
public function __construct() {
$this->contributions = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add contribution
*
* @param \ApiBundle\Entity\Report $contribution
*
* @return Employee
*/
public function addContribution(\ApiBundle\Entity\Report $contribution)
{
$this->contributions[] = $contribution;
return $this;
}
/**
* Remove contribution
*
* @param \ApiBundle\Entity\Report $contribution
*/
public function removeContribution(\ApiBundle\Entity\Report $contribution)
{
$this->contributions->removeElement($contribution);
}
/**
* Get contributions
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContributions()
{
return $this->contributions;
}
无需使用CollectionType
。解决方案是使用带有EntityType
选项的multiple
。
->add('contributors', 'entity', array(
'class' => 'ApiBundle:Employee',
'multiple' => true
))
PUT请求正文
{
"report":{
"notes":"This is a note.",
"client":5,
"contactPerson":4,
"contributors":[10,6]
}
}