以自定义形式添加父实体ID

时间:2016-12-02 21:48:51

标签: php forms doctrine-orm symfony

我使用的是由控制台crud生成器生成的自定义表单:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SubtaskType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name')->add('description')        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Subtask'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_subtask';
    }


}

生成crud后,我在TaskSubtask之间添加了双向关联,并相应地修改了控制器。因此,当我添加Subtask时,应始终将其添加到网址中的Task。以下是注释中路径的外观:

/**
 * Subtask controller.
 *
 * @Route("/task/{taskId}/subtask")
 */
class SubtaskController extends Controller
{
.
.
.
    /**
     * Creates a new subtask entity.
     *
     * @Route("/new", name="subtask_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request, $taskId)
    {
        $subtask = new Subtask();
        $form = $this->createForm('AppBundle\Form\SubtaskType', $subtask);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($subtask);
            $em->flush($subtask);

            return $this->redirectToRoute('subtask_show', array('id' => $subtask->getId(), 'taskId' => $taskId));
        }

        return $this->render('subtask/new.html.twig', array(
            'subtask' => $subtask,
            'form' => $form->createView(),
            'taskId'   => $taskId
        ));
    }
.
.
.
}

此时此代码在subtask表中成功插入一行null task_id(任务的外键)。

如何调整此代码以便插入正确的task_id

1 个答案:

答案 0 :(得分:0)

以下是我找到的工作:

我在Subtask实体

中手动添加了此方法
/**
 * Set task
 *
 * @param Task $task
 *
 * @return Subtask
 */
public function setTask($task)
{
    $this->task = $task;

    return $this;
}

而且我以这种方式修改了newAction方法:

/**
 * Creates a new subtask entity.
 *
 * @Route("/new", name="subtask_new")
 * @Method({"GET", "POST"})
 */
public function newAction(Request $request, $taskId)
{
    $subtask = new Subtask();
    $em = $this->getDoctrine()->getManager();
    $task = $em->getReference('AppBundle\Entity\Task', $taskId);
    $subtask->setTask($task);
    $form = $this->createForm('AppBundle\Form\SubtaskType', $subtask);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($subtask);
        $em->flush($subtask);

        return $this->redirectToRoute('subtask_show', array('id' => $subtask->getId(), 'taskId' => $taskId));
    }

    return $this->render('subtask/new.html.twig', array(
        'subtask' => $subtask,
        'form' => $form->createView(),
        'taskId'   => $taskId
    ));
}

这给了我预期的结果。

有更好的方法来实现这个目标吗?