【问题标题】:symfony collection populate with multiple entriessymfony 集合填充了多个条目
【发布时间】:2014-01-17 17:36:03
【问题描述】:

我正在寻找一种方法来填充我的表单集合。我正在通过原型制作自己的收藏。

在我的控制器中:

//samplecode... a person can have many cats
$em = $this->getDoctrine()->getManager();
$person= $em->getRepository('UserBundle:Person')->find(0);

$form = $this->createForm(new PersonType(), $person);

我有一个表单集合,我想用我的主要实体(人)中已经存在的所有数据(猫)填充这些表单。我的最后一种方法被注释掉了。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('cats', 'collection', array(
            'type' => new CatType(),
            'allow_add' => true,
            'allow_delete' => true,
            'mapped'=> false,
            'data_class' => 'Yoda\UserBundle\Entity\Cat',
//            'data' => $options["data"]->getCats(),
        ))
        ->getForm()
    ;
}

还有猫的FormType:

->add('name', 'text', array(
    'read_only' => true,
    'disabled' => true,
))
->add('price', 'money', array(
    'label' => 'Price',
))

提前致谢。

【问题讨论】:

    标签: symfony collections prototype populate


    【解决方案1】:

    在您的表单模板中:

    {% for cat in form.cats %}
    {{ cat.name }} {{ cat.price }}
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 2022-01-15
      • 1970-01-01
      相关资源
      最近更新 更多