symfony2 收集表格,保存到实体
symfony2 collection form, save to 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;
我当然有 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 %}
我得到一个错误:
An exception occurred while executing 'INSERT INTO skills_cfg (name,
name_short) VALUES (?, ?)' with params [null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name'
cannot be null
为什么会这样?我该如何解决?
像这样更改 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
);
}
如果还是不行,开始调试。尝试从行中获取数据并检查这是否是一个数组。
我是 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 %}
我得到一个错误:
An exception occurred while executing 'INSERT INTO skills_cfg (name, name_short) VALUES (?, ?)' with params [null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null
为什么会这样?我该如何解决?
像这样更改 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
);
}
如果还是不行,开始调试。尝试从行中获取数据并检查这是否是一个数组。