symfony2集合表单,保存到实体

时间:2015-09-13 15:00:18

标签: forms symfony collections entity

我是Symfony的新手。我想做表格的集合。 我正在做类似的事情:

class SkillType extends AbstractType
public function getName()
{
    return 'skills_cfg';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text', array(
        'label' = 'name'

    ))
        ->add('name_short', 'text', array(
            'label' => 'short name'
        ));
}

}

我得到一个表单,我希望在该表单上使用集合:

class AbsType extends AbstractType

{
private $object;

public function __construct($object)
{
    $this->object = $object;
}

public function getName()
{
    return '';
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('row', 'collection', array(
            'type' => $this->object,
            'allow_add' => true,
            'prototype' => true,
        ))
        ->add('addMore', 'button', array(
            'attr' => array(
                'value' => 'add more'
            ),
        ))
        ->add('submit', 'submit', array(
            'attr' => array(
                'value'=>'save',
            ),
            'label' => 'save'
        ));
}

}

在我的控制器中我得到了这样的表格:

class InstallController extends Controller

{

public function indexAction($configPage, Request $request)
{

    $skillForm= $this->createForm(new AbsType(new SkillType()));
    $SkillConfig=new SkillsCfg();
    if($request->isMethod('POST')) {
        $skillForm->handleRequest($request);
            if ($skillForm->isValid()) {
            $em=$this->getDoctrine()->getManager();
            $em->persist($SkillConfig);
            $em->flush();
        }
    }
    return array(
        'form' => $skillForm->createView(),
        'page' => $configPage
    );
}

实体和视图看起来像这样:

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=30)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="name_short", type="string", length=10)
 */
private $nameShort;

当然,我得到了吸气剂和制定者。

查看:

var emailCount = '{{ form.row|length }}';

jQuery(document).ready(function() {
jQuery('#addMore').click(function(e) {
e.preventDefault();

var emailList = jQuery('#fields-list');

// grab the prototype template
var newWidget = emailList.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, emailCount);
emailCount++;

// create a new list element and add it to the list
var newLi = jQuery('<li></li>').html(newWidget);
newLi.appendTo(emailList);
});
})
    </script>
{% endblock javascripts %}
{% block body %}
    {{ form_start(form) }}
    <ul id="fields-list"
    data-prototype="{{ form_widget(form.row.vars.prototype)|e }}">
        {% for newRow in form.row %}
            <li>
                {{ form_errors(newRow) }}
                {{ form_widget(newRow) }}
            </li>
        {% endfor %}
    </ul>
    {{ form_end(form) }}
{% endblock body %}

我收到错误:

  

执行'INSERT INTO skills_cfg(名称,   name_short)VALUES(?,?)'with params [null,null]:

     

SQLSTATE [23000]:完整性约束违规:1048列'名称'   不能为空

为什么会这样?我该如何解决呢?

0 个答案:

没有答案