【问题标题】:symfony2 collection form, save to entitysymfony2 集合表单,保存到实体
【发布时间】:2015-09-13 15:00:17
【问题描述】:

我是 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;

当然,我是 getter 和 setter。

查看:

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, name_short) VALUES (?, ?)' with params [null, null]:

SQLSTATE[23000]:违反完整性约束:1048 列“名称” 不能为空

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

【问题讨论】:

  • 如果我在控制器中更改该行: $skillForm= $this->createForm(new AbsType(new SkillType())); $SkillConfig=新 SkillsCfg();到 $SkillConfig=new SkillsCfg(); $skillForm= $this->createForm(new AbsType(new SkillType()),$SkillConfig);我收到错误属性“row”和方法之一“getRow()”、“row()”、“isRow()”、“hasRow()”、“__get()”都不存在并且在类中具有公共访问权限“引擎\SettingBundle\Entity\SkillsCfg”。但是我必须使用什么名称的 Collection 标签?

标签: forms symfony collections entity


【解决方案1】:

像这样更改 indexAction:

public function indexAction($configPage, Request $request)
{
    $em=$this->getDoctrine()->getManager();

    $skillForm= $this->createForm(new AbsType(new SkillsCfg()));

    $skillForm->handleRequest($request);

    if ($skillForm->isValid())
    {
        foreach($form->get('row')->getData() as $row)
        {
            $em->persist($row);
        }
        $em->flush();
    }

    return array(
        'form' => $skillForm->createView(),
        'page' => $configPage
    );
}

如果仍然无法正常工作,请开始调试。尝试从行中获取数据并检查这是否是一个数组。

【讨论】:

  • 非常感谢,我做的有点不一样,但就是这样。
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多