2

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

/**
 * @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) }}

谢谢

4

1 回答 1

1

进行原型设计时,仅当您的实体在控制器内部分配了值时才会呈现集合字段workExperience,否则您将需要使用 javascript 获取原型信息并创建输入字段,如果您想添加新字段,无论您的实体是否具有任何价值。

获取以下内容以使用值呈现

{{ 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

于 2013-10-19T17:27:24.500 回答