【发布时间】:2014-01-09 20:14:15
【问题描述】:
我定义了一个只有一个“集合”类型字段的表单:
<?php
namespace GMC\AccesoSistemaBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use GMC\AccesoSistemaBundle\Form\FuncionType;
class FuncionesType Extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('funciones', 'collection', array(
'type' => new FuncionType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false));
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => null
));
}
public function getName() {
return 'gmc_accesosistemabundle_funcionestype';
}
然后我在我的控制器中创建一个表单:
public function mostrarFuncionesAction() {
$em = $this->getDoctrine()->getManager();
$funciones = $em->getRepository('AccesoSistemaBundle:Funcion')->findAll();
$formulario = $this->createForm(new FuncionesType(), $funciones);
return $this->render(
'AccesoSistemaBundle:Default:funciones.html.twig',
array('formulario' => $formulario->createView())
);
}
但是即使 $funciones 有两条记录,表单的 'funciones' 集合也是空的,为什么?我错过了什么吗?
如您所见,我是 Symfony2 的新手,所以请耐心等待。
提前感谢您的帮助!
【问题讨论】:
标签: forms symfony collections