symfony 3中的外键null

时间:2017-05-18 16:04:29

标签: php symfony doctrine-orm symfony-forms

我有两个实体Survey.phpChoice.php我创建了一个表单来添加新的调查和多项选择,我在两个实体之间使用了多对一的关系,问题出在我提交时对于新的调查,选择实体的外键返回null

这是我的代码

Survey.PHP

/**
 * Survey
 *
 * @ORM\Table(name="survey")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\SurveyRepository")
 */
class Survey
{
 /....  

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Choice", mappedBy="survey_id",cascade="persist")
     * @ORM\JoinColumn(nullable=false, referencedColumnName="id")
     */
    private $choice;

    public function __construct()
    {
        $this->choice = new ArrayCollection();
    }



    /**
     * Add choice
     *
     * @param \AppBundle\Entity\Choice $choice
     *
     * @return Survey
     */
    public function addChoice(\AppBundle\Entity\Choice $choice)
    {
        $this->choice[] = $choice;
        $choice->setSurvey($this);
        return $this;
    }

    /**
     * Remove choice
     *
     * @param \AppBundle\Entity\Choice $choice
     */
    public function removeChoice(\AppBundle\Entity\Choice $choice)
    {
        $this->choice->removeElement($choice);
    }

    /**
     * Get choice
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChoice()
    {
        return $this->choice;
    }
}

Choice.php

/**
 * Choice
 * @ORM\Table(name="choice")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ChoiceRepository")
 */
class Choice
{

    /**
     * @var int
     * @ORM\ManyToOne(targetEntity="Survey",inversedBy="choice")
     */
    private $survey;



    /**
     * Set survey
     *
     * @param \AppBundle\Entity\Survey $survey
     *
     * @return Choice
     */
    public function setSurveyId(\AppBundle\Entity\Survey $survey)
    {
        $this->survey = $survey;

        return $this;
    }

    /**
     * Get surveyId
     *
     * @return \AppBundle\Entity\Survey
     */
    public function getSurveyId()
    {
        return $this->survey_id;
    }
}

SurveyController.php

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Survey;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
 * Survey controller.
 *
 */
class SurveyController extends Controller
{

    /**
     * Creates a new survey entity.
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function newAction(Request $request)
    {
        $survey = new Survey();
        //
        $form = $this->createForm('AppBundle\Form\SurveyType', $survey);
        $form->handleRequest($request);
        $survey->setCreateDate(new \DateTime('NOW'));

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


                return $this->redirectToRoute('survey_show', ['id' => $survey->getId()]);
            }


        return $this->render('survey/new.html.twig', [
            'survey' => $survey,
            'form' => $form->createView(),
        ]);
    }

任何建议,顺便说一句,我认为问题出在吸气剂和制定者身上)

3 个答案:

答案 0 :(得分:1)

将选择与调查联系起来:

// Survey
public function addChoice(\AppBundle\Entity\Choice $choice)
{
    $this->choice[] = $choice;
    $choice->setSurvey($this);
    return $this;
}

并将survey_id内容更改为调查。处理对象而不是ID。当然,Survey :: choice应该是Survey :: choices。名称更改可能看起来很小,但会使您更容易维护。

答案 1 :(得分:0)

我通过在SurveyController.php中为每个循环添加一个来解决问题 它工作得很好

SurveyController.php

if ($form->isSubmitted() && $form->isValid())
        {
            foreach ($survey->getChoices() as $choice){
                $choice->setSurvey($survey);
            }

            $em = $this->getDoctrine()->getManager();
            $em->persist($survey);
            $em->flush();

不是&#34;最好的解决方案&#34;但它完成了工作

答案 2 :(得分:0)

它对我有用,但我必须从类定义中删除带有“inversedBy”设置的显式外键映射。我使用复合外键(使用两列),但这可能会使事情变得更难...