- 從Generated Entities現有數據庫
- 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
你在'Question'實體類中做任何初始化嗎? –
@OscarPérez,是的,只是創建QuestionCategory實體 – sectus
然後這就是問題所在。你正在設置一個不被管理的'QuestionCategory'。你可以添加構造函數的代碼嗎? –