1

我正在 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(),
        ));
    }
}
}

请帮助我,我到处搜索,但没有:(。

4

1 回答 1

1
Catchable Fatal Error: Object of class Acme\BlogBundle\Entity\Disease 
could not be converted to string.

$parent属性在您的实体中声明为字符串,在您的表单中声明为实体。


要解决此问题,您有 2 个选项:

选项1

您创建一个OneToOne 关系,自我引用。

/**
 * @OneToOne(targetEntity="Acme\BlogBundle\Entity\Disease")
 * @JoinColumn(name="disease_id", referencedColumnName="id")
 */
private $parent;

选项 2

您为父属性创建一个数据转换器,以将疾病实体转换为字符串。


您决定采用哪个选项取决于您在您的$parent财产中需要什么,如果您需要一个字符串,请转到选项 2,如果您需要存储整个实体,请使用选项 1。有点难以理解为什么您需要这样做尽管..

于 2013-02-05T12:48:52.877 回答