呈现传递给Doctrine数组字段的文本字段集合

时间:2013-10-18 09:51:09

标签: symfony symfony-forms symfony-2.3

在我的实体中,我有一个数组字段:

/**
 * @var array
 *
 * @ORM\Column(name="work_experience", type="array")
 */
private $workExperience;

现在我想呈现一组文本字段,这些字段将传递给此数组字段。

->add('workExperience', 'collection', array(
                'type'         => 'text',
                'allow_add'    => true,
                'allow_delete' => true,
                'prototype'    => true,
                #'by_reference' => false,
                'options'  => array(
                    'required'  => false,
                    'attr'      => array('class' => 'line-box')
                ),
            ))

但是现在当我渲染这个字段时,没有显示输入?我的错是什么?

{{ form_row(form.workExperience) }}

谢谢

1 个答案:

答案 0 :(得分:1)

在进行原型设计时,只有当您的实体在控制器中有一个赋值给workExperience的值时,才会呈现集合字段,否则您需要使用javascript来获取原型信息并创建输入字段( s),如果你想在你的实体有或没有任何价值的情况下添加新的字段,也是如此。

要使用值

进行渲染
{{ form_row(form.workExperience) }}

您可以执行以下操作:

public function controllerAction(Request $request)
{
    //By populating your entity with values from your database
    //workExperience should receive a value and be rendered in your form.
    $em = $this->getDoctrine()->getManager();
    $entity = $em
       ->getRepository('yourBundle:entity')
       ->findBy(...yourParameters...);


$form = $this->createForm('your_form_type', $entity);
...

或者

...
//If you do not have any data in your database for `workExperience` 
//then you would need to set it in your controller.

$arr = array('email' => 'name@company.com', 'phone' => '888-888-8888');
$entity->setWorkExperience($arr);
$form = $this->createForm('your_form_type', $entity);
...

请记住,集合通常用于一对多或多对多关系。 可以将它用于数组,但没有太多记录。虽然这个链接不是一个完美的选择,但提出的一般想法很有帮助:form_collections