我必须在注册表格时保存一个值。在控制器中我有这个:
<?php
$heritageBuilder = $this->createFormBuilder($heritage)
->add('category', 'choice')
->add('designation', 'text')
->add('value', 'text');
$heritageFormBuilder = $heritageBuilder->getForm();
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$heritageFormBuilder->bindRequest($request);
...
}
?>
我收到了这个:
array(2) {
["form"]=>
array(4) {
["category"]=>
string(1) "4"
...
它不起作用,因为类别是另一个表类型HeritageCategory的外键, 因此我对收到的值进行了如下修改,正确的格式是:
<?php
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$post = $request->request->get('form');
$post['category'] = $em->getRepository('PrismeMigrationBundle:HeritageCategory')->find((int)$post['category']);
$request->request->set('form', $post);
$heritageFormBuilder->bindRequest($request);
}
?>
我重视我的要求中的许多变化:
["form"]=>
array(4) {
["category"]=>
object(Test\MonBundle\Entity\HeritageCategory)#427 (6) {
["id":"Test\MonBundle\Entity\HeritageCategory":private]=>
int(4)
["parent":"Test\MonBundle\Entity\HeritageCategory":private]=>
int(3)
["code":"Test\MonBundle\Entity\HeritageCategory":private]=>
string(0) ""
["name":"Test\MonBundle\Entity\HeritageCategory":private]=>
string(21) "Résidence principale"
["bg":"Test\MonBundle\Entity\HeritageCategory":private]=>
int(4)
["bd":"Test\MonBundle\Entity\HeritageCategory":private]=>
int(5)
}
但我收到此错误: “标量”类型的预期参数,“Prisme\MigrationBundle\Entity\HeritageCategory”给出
正确完成对 BindRequest 的修改后,我应该怎么做?
先感谢您