How to avoid “Entities passed to the choice field must be managed. Maybe persist them in the entity manager?”

前端 未结 5 1779
既然无缘
既然无缘 2020-12-20 19:56
  1. Generated Entities from existing database
  2. Generated CRUD controller

But it does not work with exception message:

Entit

5条回答
  •  不知归路
    2020-12-20 20:53

    According to the code shown on your GitHub project, the Question entity has the following constructor:

    public function __construct() 
    {
        $this->questionCategory = new QuestionCategory();
    }
    

    When you create an entity form field, it can only contain values that are managed by doctrine, but your new questionCategory is not managed.

    Usually, the best solution is just to not fill this entity field in the constructor, but only in those places you strictly need it. When building a form, Synfony will fill it for you after submitting and calling $form->handleRequest().

    So, in your case, just remove the Question entity's constructor.

    After that, you'll also need to implement the __toString() method in QuestionCategory entity:

     public function __toString(){
           return 'whatever you neet to see the type`;
     }
    

提交回复
热议问题