我有一个"任务"表,引用一个"Estados"表,其中包含从Tasks.taskestado列到Estados.estado的外键.
这是任务的相关XML映射:
... ...
而对于Estados:
...
鉴于此,我正在尝试创建一个动作(novaAction())来创建任务.这是Controller代码:
public function novaAction(Request $request) { $task = new Tasks(); $em = $this->getDoctrine()->getManager(); dump($task); #$task->setTaskEstado(new Estados()); $form = $this->createForm(TasksType::class, $task); $form->handleRequest($request); if ($form->isSubmitted()) { if ($form->isValid()) { // Criar a tarefa na BD $em->persist($form->getData()); $em->flush(); $this->addFlash('notice', 'app.nova_tarefa.mensagem_sucesso'); return $this->redirectToRoute('nova_tarefa'); } $this->addFlash('error', 'app.nova_tarefa.mensagem_erro'); }
以及相关的TasksType代码:
class TasksType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('taskid', HiddenType::class) ... #->add('taskestado') ->add('taskestado', EntityType::class, [ 'class' => 'AppBundle:Estados' ]) ... } /** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Tasks', #'empty_data' => function (\Symfony\Component\Form\FormInterface $form) { # #return new Tasks($form->get('tasks')->getData()); # return new Tasks(); #}, )); } }
'empty_data'选项是尝试创建表单而不向其传递任务实例.另外,当我使用注释代码添加taskestado时,我也有相同的结果,即没有参数.
这是相关的任务实体:
namespace AppBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; /** * Tasks */ class Tasks { /** * @var integer */ protected $taskid; /** * @var \AppBundle\Entity\Estados */ protected $taskestado; ... /** * Get taskid * * @return integer */ public function getTaskid() { return $this->taskid; } /** * Set taskestado * * @param \AppBundle\Entity\Estados $taskestado * * @return Tasks */ public function setTaskestado(\AppBundle\Entity\Estados $taskestado = null) { $this->taskestado = $taskestado; return $this; } /** * Get taskestado * * @return \AppBundle\Entity\Estados */ public function getTaskestado() { return $this->taskestado; } ... }
当我打开页面时,出现以下错误:
必须管理传递到选择字段的实体.也许坚持他们在实体经理?
我还生成了CRUD(bin/console doctrine:generate:crud --filter=Tasks
),以便我可以检查它是如何完成的,但代码是相似的,以及结果(在某些日期时间列的TasksType中修复了一些问题之后).
我究竟做错了什么?