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

前端 未结 5 1766
既然无缘
既然无缘 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

    This error means the attribute questionCategory which is a relationship, is not managed by the EntityManager. For this to be done automatically, add a cascade-persist in your Doctrine Mapping for questionCategory attribute:

    Entity

    /**
     * Question
     *
     * @ORM\Table(name="question")
     * @ORM\Entity
     */
    class Question
    {
        //...
    
        /**
         * @ORM\ManyToOne(
         *       targetEntity="QualityBundle\Entity\QuestionCategory", 
         *       cascade={"persist"}
         * )
         */
        private $questionCategory;
    
        //...
    }
    

    This way, when you call $em->persist($question);, the QuestionCategory linked to your Question will automatically be persisted as well.

提交回复
热议问题