如何避免“必须管理传递到选项字段的实体.也许将它们保留在实体管理器中?


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

  1. 从现有数据库生成的实体
  2. 生成的 CRUD 控制器

但它不适用于异常消息:

必须管理传递到选择字段的实体。也许将它们保留在实体管理器中?

实体

/**
 * Question
 *
 * @ORM'Table(name="question", indexes={@ORM'Index(name="question_category_id", columns={"question_category_id"})})
 * @ORM'Entity
 */
class Question
{
    //...
    /**
     * @var 'AppBundle'Entity'QuestionCategory
     * @ORM'ManyToOne(targetEntity="AppBundle'Entity'QuestionCategory")
     * @ORM'JoinColumns({
     *   @ORM'JoinColumn(name="question_category_id", referencedColumnName="id")
     * })
     */
    private $questionCategory;
    public function __construct() 
    {
        $this->questionCategory = new QuestionCategory();
    }
    //...
}

形式

class QuestionType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('questionCategory');
    }
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle'Entity'Question'
        ));
    }
}

控制器

class QuestionController extends Controller
{
   //...
   /**
     * Creates a new Question entity.
     * @Route("/new", name="question_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $question = new Question();
        $form = $this->createForm('AppBundle'Form'QuestionType', $question);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($question);
            $em->flush();
            return $this->redirectToRoute('question_show', array('id' => $question->getId()));
        }
        return $this->render('question/new.html.twig', array(
            'question' => $question,
            'form' => $form->createView(),
        ));
    }
//...
}

深度调试对我没有任何帮助。如何解决?

要重现错误的测试存储库:https://github.com/sectus/question.test.local

请注意,在表单类型中使用'by_reference' => false,与未ManyToMany的关系时,也会引发此错误。
不幸的复制/粘贴使我处于这种情况。

根据 GitHub 项目中显示的代码,Question实体具有以下构造函数:

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

当您创建entity表单域时,它只能包含由原则管理的值,但不能管理您的新questionCategory

通常,最好的解决方案是不在构造函数中填充此实体字段,而仅在您严格需要它的地方填充。在构建表单时,Synfony将在提交并致电$form->handleRequest()后为您填写。

因此,在您的情况下,只需删除Question实体的构造函数。

之后,您还需要在实体中实现__toString()方法QuestionCategory

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

就我而言,我遇到了这个问题,因为我使用 EntityType 而不是 ChoiceType 来构建 selectList。

EntityType仅显示数据库中的数据,ChoiceType可以显示"非托管"对象。

我希望这会有所帮助。

此错误

表示作为关系的属性questionCategory不受实体管理器管理。要自动完成此操作,请在 questionCategory 属性的教义映射中添加级联持久

实体

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

这样,当您调用$em->persist($question);时,链接到QuestionQuestionCategory也将自动持久化。

造成此问题的原因有很多。

就我而言,我修复了它的变化

by_reference' => 假

by_reference' => 真

在 CRUD 控制器中。

就我而言,这只是因为我的错,我正在使用QueryManager而不是EntityManager来查找控制器中的实体。