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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度