I am learning Symfony and I ran into this problem.
Catchable Fatal Error: Object of class App\Entity\Question could not be converted to string
My goal is to add via the form to the database.
I think I am using my EntityType wrong, here is my form:
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
)
;
}
And here is my Controller where I build my form:
$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()
];
I am guessing the database structure is also important here:
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)
describe answer;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| question_id | int(11) | YES | MUL | NULL | |
| answer | varchar(255) | NO | | NULL | |
| valid | tinyint(1) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
I hope I structured my question well, this is my first question on stack overflow.
Thank you for your help.