Php 如何使复选框在Symfony 2中工作?
我目前正在创建一个表单,允许用户从下拉列表中选择特定的技能,并为嗜好设置一个复选框,让用户可以根据自己的喜好进行检查 这是我的表格:Php 如何使复选框在Symfony 2中工作?,php,symfony,doctrine-orm,twig,Php,Symfony,Doctrine Orm,Twig,我目前正在创建一个表单,允许用户从下拉列表中选择特定的技能,并为嗜好设置一个复选框,让用户可以根据自己的喜好进行检查 这是我的表格:简历 /* namespace ........... */ use Doctrine\ORM\Mapping as ORM; /** * CurriculumVitae * * @ORM\Table(name="foo_cv") * @ORM\Entity */ class CurriculumVitae { /** * @var intege
简历
/* namespace ........... */
use Doctrine\ORM\Mapping as ORM;
/**
* CurriculumVitae
*
* @ORM\Table(name="foo_cv")
* @ORM\Entity
*/
class CurriculumVitae
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
* @ORM\ManyToOne(targetEntity="Foo\BarBundle\Entity\Skills")
* @ORM\JoinColumn(name="skills", referencedColumnName="id")
*/
private $skills;
/**
* @var integer
* @ORM\ManyToOne(targetEntity="Foo\BarBundle\Entity\Hobby", cascade={"persist"})
* @ORM\JoinColumn(name="hobbies", referencedColumnName="id")
*/
private $hobby;
/* Setters and Getters ....... */
}
以下是我的表单类型的一些代码:简历类型
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('skills', 'entity', array('class' =>'FooBarBundle:Skills','property' => 'skills'))
->add('hobby', 'entity', array( 'class' => 'FooBarBundle:Hobby','property' => 'hobbies', 'expanded'=>true,'multiple'=>true, 'label' => 'hobbies'))
->add('save','submit',array('label'=>'Submit'))
;
}
/* OptionsResolverInterface ..... */
/* getName() .... */
我在我的小树枝中这样调用我的表单:cv.twig.html
{{ form(curriculumForm) }}
最后是我的控制器:课程控制器
$em = $this->getDoctrine()->getManager();
$cv = new CurriculumVitae();
$curriculumForm = $this->createForm(new CurriculumVitaeType(), $cv);
$curriculumForm->handleRequest($request);
if ($curriculumForm->isValid()) {
$em->persist($cv);
$em->flush();
return $this->redirect($this->generateUrl('foo_main_window'));
}
return array('curriculumForm'=> $curriculumForm->createView());
表单显示正确,但当我从下拉列表中选择一项技能并指定某项爱好并单击提交时,会抛出一个错误
在关联Foo\BarBundle\entity\CertumVitae\hobby上找到类型为Doctrine\Common\Collections\ArrayCollection的实体,但应为Foo\BarBundle\entity\hobby
我不知道我是否遗漏了什么,但我认为错误发生在提交表单后保存数据的过程中。这是因为您有多对一关系,这意味着 许多
个人简历
都可以(相同)单身爱好
/* namespace ........... */
use Doctrine\ORM\Mapping as ORM;
/**
* CurriculumVitae
*
* @ORM\Table(name="foo_cv")
* @ORM\Entity
*/
class CurriculumVitae
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
* @ORM\ManyToOne(targetEntity="Foo\BarBundle\Entity\Skills")
* @ORM\JoinColumn(name="skills", referencedColumnName="id")
*/
private $skills;
/**
* @var integer
* @ORM\ManyToOne(targetEntity="Foo\BarBundle\Entity\Hobby", cascade={"persist"})
* @ORM\JoinColumn(name="hobbies", referencedColumnName="id")
*/
private $hobby;
/* Setters and Getters ....... */
}
但另一方面,您在表单中创建了一个带有选项'multiple'=>true
的字段,这意味着您允许用户选择多个爱好。因此,表单返回Hobby
实体的ArrayCollection
,而不是单个实例
那不匹配。您需要删除
multiple
选项,或者在$hobby
属性上建立多对多关系。我的简历实体中是否也应该包含构造函数?/***建造商。*/公共函数_construct(){$this->hobby=new ArrayCollection();}如果它是一个集合,那么是。但我不知道你的预期结果是什么,所以我无法建议你应该走哪条路。我的主要目标是从下拉列表中选择一项技能,并为其指定爱好。:)如果是这样,你需要建立多对多关系,而不是多对一关系。。非常感谢:)