2014-01-09 38 views
1

我定義的形式,只有一個「收集」類型的字段:空集字段類型

<?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的完整newbbie,請耐心等待我。

在此先感謝您的幫助!

回答

0

什麼Symfony的與您當前的代碼做的是:

  • 採取$ functiones對象(功能類的實例)
  • 尋找一個屬性,功能類中調用functiones
  • 滋潤你的表格在此屬性中找到的數據

如果您要使用實體mappin,則必須將您的表單字段(與$builder->add('<name_here>'))命名爲你的Function類的屬性,這是Function的集合。

否則,你可以嘗試用一個數組來滋潤你的單,寫明:

$formulario = $this->createForm(new FuncionesType(), array('functiones' => $funciones)); 
+0

您的解決方案就像一個魅力maphe。真的非常感謝你! –