duanqin9631
2019-04-13 19:31
浏览 60
已采纳

Symfony - Catchable Fatal Error:类App \ Entity \ Question的对象无法转换为字符串

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.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongtuo4723 2019-04-13 20:05
    最佳回答

    I admire how Symfony has everything very well documented, and your case is not an exception. See the docs for Entity Type.

    It has a field option choice_label:

    This is the property that should be used for displaying the entities as text in the HTML element.

    If left blank, the entity object will be cast to a string and so must have a __toString() method. You can also pass a callback function for more control.

    So, you do not specify choice_label, and you do not have __toString implemented, hence the error.

    What you should do:

    $builder
        ->add('question', EntityType::class,
        [
            'class' => Question::class,
            'choice_label' => 'questionName',
        ])
    

    OR implement __toString() in Entity\Question.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题