我有两个实体Survey.php
,Choice.php
我创建了一个表单来添加新调查和多项选择,我在两个实体之间使用了多对一关系,问题是当我提交新调查时,选择实体的外键返回 null
这是我的代码
调查.PHP
/**
* Survey
*
* @ORMTable(name="survey")
* @ORMEntity(repositoryClass="AppBundleRepositorySurveyRepository")
*/
class Survey
{
/....
/**
* @var ArrayCollection
* @ORMOneToMany(targetEntity="AppBundleEntityChoice", mappedBy="survey_id",cascade="persist")
* @ORMJoinColumn(nullable=false, referencedColumnName="id")
*/
private $choice;
public function __construct()
{
$this->choice = new ArrayCollection();
}
/**
* Add choice
*
* @param AppBundleEntityChoice $choice
*
* @return Survey
*/
public function addChoice(AppBundleEntityChoice $choice)
{
$this->choice[] = $choice;
$choice->setSurvey($this);
return $this;
}
/**
* Remove choice
*
* @param AppBundleEntityChoice $choice
*/
public function removeChoice(AppBundleEntityChoice $choice)
{
$this->choice->removeElement($choice);
}
/**
* Get choice
*
* @return DoctrineCommonCollectionsCollection
*/
public function getChoice()
{
return $this->choice;
}
}
选择.php
/**
* Choice
* @ORMTable(name="choice")
* @ORMEntity(repositoryClass="AppBundleRepositoryChoiceRepository")
*/
class Choice
{
/**
* @var int
* @ORMManyToOne(targetEntity="Survey",inversedBy="choice")
*/
private $survey;
/**
* Set survey
*
* @param AppBundleEntitySurvey $survey
*
* @return Choice
*/
public function setSurveyId(AppBundleEntitySurvey $survey)
{
$this->survey = $survey;
return $this;
}
/**
* Get surveyId
*
* @return AppBundleEntitySurvey
*/
public function getSurveyId()
{
return $this->survey_id;
}
}
测量控制器.php
<?php
namespace AppBundleController;
use AppBundleEntitySurvey;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
/**
* Survey controller.
*
*/
class SurveyController extends Controller
{
/**
* Creates a new survey entity.
* @param Request $request
* @return SymfonyComponentHttpFoundationRedirectResponse|SymfonyComponentHttpFoundationResponse
*/
public function newAction(Request $request)
{
$survey = new Survey();
//
$form = $this->createForm('AppBundleFormSurveyType', $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(),
]);
}
任何建议,顺便说一句,我认为问题出在获取者和二传手(
将选择链接到调查:
// Survey
public function addChoice(AppBundleEntityChoice $choice)
{
$this->choice[] = $choice;
$choice->setSurvey($this);
return $this;
}
并更改要调查survey_id内容。 处理对象而不是 id。 当然,Survey::choice应该是Survey::choices。 名称更改可能看起来很小,但会让您更易于维护。
我通过在 SurveyController 中的每个循环中添加一个来解决这个问题.php它工作得很好
勘测控制器.php
if ($form->isSubmitted() && $form->isValid())
{
foreach ($survey->getChoices() as $choice){
$choice->setSurvey($survey);
}
$em = $this->getDoctrine()->getManager();
$em->persist($survey);
$em->flush();
不是"最佳解决方案",但它可以完成工作
它对我有用,但我不得不从类定义中删除带有"inversedBy"设置的显式外键映射。我使用复合外键(使用两列(,这可能会使事情变得更加困难......