【问题标题】:How to avoid "Entities passed to the choice field must be managed. Maybe persist them in the entity manager?"如何避免“必须管理传递给选择字段的实体。也许将它们保留在实体管理器中?”
【发布时间】:2016-02-17 07:55:37
【问题描述】:
  1. Generated Entities 来自现有数据库
  2. Generated 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

【问题讨论】:

  • 你在Question实体类里做初始化吗?
  • @OscarPérez,是的,只是创建 QuestionCategory 实体
  • 那么这就是问题所在。您正在设置一个不受管理的 QuestionCategory。能否请您添加构造函数的代码?
  • @OscarPérez,我已经创建了您可以探索的测试存储库:github.com/sectus/question.test.local

标签: php doctrine-orm symfony


【解决方案1】:

请注意,在您的表单类型中使用 'by_reference' => false, 且关系不是 ManyToMany 时,也会引发此错误。
不幸的复制/粘贴让我陷入了这种境地。

【讨论】:

    【解决方案2】:

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

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

    当您创建entity 表单字段时,它只能包含由学说管理的值,但您的新questionCategory 不受管理。

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

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

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

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

    【讨论】:

    • 没有它我还有其他错误“类 AppBundle\Entity\QuestionCategory 的对象无法转换为字符串”
    • 非常感谢。太明显了......就像我的脑海中的日食。
    • @sectus,你是什么意思?
    • 我有关于对象到字符串转换的错误(X 问题)。我已经创建了 contrucor 并遇到了其他错误(Y 问题)。我没有用另一种方法解决 X,而是尝试解决 Y。
    • 啊,我明白了,对于 XY,我认为这是关于 DNA 的东西...... :-D
    【解决方案3】:

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

    EntityType 只显示来自数据库的数据,而ChoiceType 可以显示“未托管”的对象。

    我希望这会有所帮助。

    【讨论】:

    • 感谢您指出这一点。就我而言,我正在缓存我的实体以预填充 EntityType 字段。现在我正在缓存标识符并使​​用EntityRepository::find() 在我的data 字段选项中获取实体。
    【解决方案4】:

    这个错误表示属性questionCategory是一个关系,不是由EntityManager管理的。要自动完成此操作,请在您的 Doctrine Mapping 中为 questionCategory 属性添加 cascade-persist

    实体

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

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

    【讨论】:

    • 什么都没有改变 width persist :^ ( 用 null 替换了 persist ,我得到了带有文本框而不是 select 的表单。另外,我不想从 QuestionCategory 更改记录。
    【解决方案5】:

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

    在我的情况下,我修复了它的变化

    by_reference' => 错误

    by_reference' => 是的

    在 CRUD 控制器中。

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-14
        • 1970-01-01
        • 1970-01-01
        • 2012-03-09
        • 2012-06-28
        • 1970-01-01
        • 2014-07-15
        • 1970-01-01
        相关资源
        最近更新 更多