我正在学习Symfony,遇到了这个问题。
可捕获的致命错误:无法将类App \ Entity \ Question的对象转换为字符串
我的目标是通过表单添加到数据库中。
我想我使用的EntityType错误,这是我的表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('question', EntityType::class,
[
'class' => Question::class
]
)
->add(
'answer',
TextType::class
)
->add(
'valid',
ChoiceType::class,
[
'choices' => [
'true' => 1,
'false' => 0
]
]
)
->add(
'save',
SubmitType::class
)
;
}
这是我的控制器,我在其中构建表单:
$entityManager = $this->getDoctrine()->getManager();
$answer = new Answer();
$form = $this->createForm(ExamDatabaseInteractionType::class, $answer);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$entityManager->persist($answer);
$entityManager->flush();
}
return [
'form' => $form->createView()
];
我猜数据库结构在这里也很重要:
describe question;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| question | varchar(255) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
我希望我的问题结构合理,这是我关于堆栈溢出的第一个问题。
qq_笑_17