我正在Symfony 2标准版中构建一个项目。
我制作了一个表单来插入我的疾病实体,我有2个选择框,一个是名为group的ManyToOne字段,链接到另一个名为Groups的表的id,另一个是链接到同一Disease表的父。
Selectbox组工作正常并发送一个普通变量,但父选择框doesent似乎在这里发送任何内容是错误代码
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
My 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(),
));
}
}
}
请帮帮我,我到处都搜索,但没有:(。
答案 0 :(得分:1)
Catchable Fatal Error: Object of class Acme\BlogBundle\Entity\Disease
could not be converted to string.
$parent
属性在您的实体中声明为字符串,并在表单中声明为实体。
要解决此问题,您有两个选择:
您创建了OneToOne relationship,自我引用。
/**
* @OneToOne(targetEntity="Acme\BlogBundle\Entity\Disease")
* @JoinColumn(name="disease_id", referencedColumnName="id")
*/
private $parent;
您为父属性创建data transformer,以将疾病实体转换为字符串。
您决定选择哪个选项取决于您在$parent
属性中需要什么,如果您需要字符串转到选项2,如果您需要存储整个实体,请选择选项1.有点困难明白为什么你需要这样做..