必须避免如何避免传递给选择字段的实体.也许将它们保留在实体管理器中?" [英] How to avoid "Entities passed to the choice field must be managed. Maybe persist them in the entity manager?"

查看:64
本文介绍了必须避免如何避免传递给选择字段的实体.也许将它们保留在实体管理器中?"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 从现有数据库生成的实体
  2. 生成的CRUD 控制器
  1. Generated Entities from existing database
  2. Generated CRUD controller

但是它不适用于异常消息:

But it does not work with exception message:

必须管理传递给选择字段的实体.也许将它们保留在实体经理中?

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

实体

/**
 * 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(),
        ));
    }

//...
}

深度调试无济于事.如何解决?

Deep debugging gives nothing to me. How to fix it?

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

Test repository to reproduce error: https://github.com/sectus/question.test.local

推荐答案

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

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

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

创建entity表单字段时,该字段只能包含由准则管理的值,而新的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.

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

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().

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

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

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

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`;
 }

这篇关于必须避免如何避免传递给选择字段的实体.也许将它们保留在实体管理器中?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆