2016-02-17 32 views
1
  1. Generated Entities現有數據庫
  2. Generated CRUD控制器

不過,這並不與異常消息工作:如何避免「必須管理傳遞給選擇字段的實體,可能會將其留在實體管理器中?」

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

//... 
} 

深調試給我什麼都不是。如何解決它?

測試庫來再現錯誤:https://github.com/sectus/question.test.local

+0

你在'Question'實體類中做任何初始化嗎? –

+0

@OscarPérez,是的,只是創建QuestionCategory實體 – sectus

+0

然後這就是問題所在。你正在設置一個不被管理的'QuestionCategory'。你可以添加構造函數的代碼嗎? –

回答

5

根據您的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`; 
} 
+0

沒有它我有其他錯誤「AppBundle \ Entity \ QuestionCategory類的對象無法轉換爲字符串」 – sectus

+0

好的,更新的答案 –

+0

非常感謝這很明顯...這就像我的腦海裏的日蝕 – sectus

1

此錯誤意味着屬性questionCategory這是一個關係,不被的EntityManager管理。對於這個自動完成,加在你的學說映射級聯堅持questionCategory屬性:

實體

/** 
* Question 
* 
* @ORM\Table(name="question") 
* @ORM\Entity 
*/ 
class Question 
{ 
    //... 

    /** 
    * @ORM\ManyToOne(
    *  targetEntity="QualityBundle\Entity\QuestionCategory", 
    *  cascade={"persist"} 
    *) 
    */ 
    private $questionCategory; 

    //... 
} 

這樣,當你調用$em->persist($question);QuestionCategory鏈接到您的Question也會自動保留。

+0

沒有改變寬度持久性:^(替換爲null,並且我已經獲得了表單而不是選擇。此外,我不想改變QuestionCategory的記錄。 – sectus

0

在我而言,它只是因爲我的錯,我用的QueryManager代替EntityManager找到實體在我的控制器中。

+0

這不提供這個問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18481681) – Sammitch