我正在 Symfony 2 标准版中构建一个项目。
我制作了一个表格来插入我的疾病实体,我在该表格中有 2 个选择框,一个是名为 group 的 ManyToOne 字段,链接到另一个名为 Groups 的表的 id,另一个是链接到同一个疾病表的父级。
选择框组工作正常并发送一个正常的变量,但父选择框似乎没有发送任何东西这里是错误代码
An exception occurred while executing
'INSERT INTO disease (parent, name, latin_name, code, notice, modified, group_id) VALUES (?, ?, ?, ?, ?, ?, ?)'
with params {"1":{},"2":"Alamanja","3":"mirkus","4":"A011","5":"sad sada","6":"2012-01-01 00:00:00","7":"1"}:
Catchable Fatal Error: Object of class Acme\BlogBundle\Entity\Disease could not be converted to string in D:\xampp\htdocs\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 138
我的 Form 对象看起来像这样
namespace Acme\BlogBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class DiseaseType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\BlogBundle\Entity\Disease',
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('latinName');
$builder->add('code');
$builder->add('notice', 'textarea');
$builder->add('parent', 'entity', array(
'class' => 'AcmeBlogBundle:Disease',
'property' => 'name',
'empty_value' => '--Izaberi grupu--',
));
$builder->add('group', 'entity', array(
'class' => 'AcmeBlogBundle:Groups',
'query_builder' => function($repository) { return $repository->createQueryBuilder('p')->orderBy('p.id', 'ASC'); },
'property' => 'name',
));
$builder->add('modified', null, array('widget' => 'single_text'));
}
public function getName()
{
return 'disease';
}
}
我从我的控制器调用我的表单对象
namespace Acme\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\BlogBundle\Entity\Groups;
use Acme\BlogBundle\Entity\Disease;
use Symfony\Component\HttpFoundation\Request;
use Acme\BlogBundle\Form\Type\BlogType;
use Acme\BlogBundle\Form\Type\DiseaseType;
class DiseaseController extends Controller
{
public function newAction(Request $request)
{
// create a task and give it some dummy data for this example
$disease = new Disease();
$form = $this->createForm(new DiseaseType(), $disease);
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($disease);
$em->flush();
return $this->redirect($this->generateUrl('disease_show'));
}
}else{
return $this->render('AcmeBlogBundle:Default:newDisease.html.twig', array(
'form' => $form->createView(),
));
}
}
}
请帮助我,我到处搜索,但没有:(。