我有一个看起来很难描述的问题,但无论如何我都会尝试。
在我的MatchesResultscontroller中,我有以下代码来构建一个实体:
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
if($this->getRequest()->isPost()) {
// get id of current match
$match_id = (int)$this->params()->fromRoute('match', 1);
// find match based on current match id
$results = $em->getRepository('Competitions\Entity\MatchesResults')->findBy(
['match_id' => $match_id]
);
$matchresults = new \Competitions\Entity\MatchesResults();
// Input
$matchresults->stek = $this->getRequest()->getPost('Res_Stek');
$matchresults->member_id = $this->getRequest()->getPost('Res_Name');
$matchresults->weight = $this->getRequest()->getPost('Res_Gewicht');
$matchresults->points = $this->getRequest()->getPost('Res_Punten');
$matchresults->match_id = $match_id;
$matchresults->amount = $this->getRequest()->getPost('Res_Aantal');
// Add
$em->persist($matchresults);
$em->flush($matchresults);
// Redirect back to the competition overview
return $this->redirect()->toRoute('admin-match');
文件MatchesResults.php看起来像这样
<?php
namespace Competitions\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
@ORM\Table()
@ORM\Entity
@ORM\Table(name="matches_results")
*/
class MatchesResults {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
public $id;
/** @ORM\Column(type="integer") */
public $match_id;
/** @ORM\Column(type="integer") */
public $member_id;
/** @ORM\Column(type="integer") */
public $stek;
/** @ORM\Column(type="integer") */
public $weight;
/** @ORM\Column(type="integer") */
public $amount;
/** @ORM\Column(type="float") */
public $points;
/** @ORM\Column(type="integer") */
public $position;
public $var;
/**
* @ORM\OneToMany(targetEntity="Competitions\Entity\Matches", mappedBy="Members")
* @ORM\JoinColumn(name="match_id", referencedColumnName="id")
*/
public $result;
public function getResult() {
return $this->result;
}
/**
* @ORM\OneToOne(targetEntity="Members\Entity\Members")
* @ORM\JoinColumn(name="member_id", referencedColumnName="user_id") <----- problem here
*/
public $member;
public function getMember() {
return $this->member;
}
}
当我需要概述给定匹配的所有结果时,如果正确的用户链接到该结果,它就能完美地运行。 但是,当我需要添加一个新结果时,我得到一个member_id = null,因为doctrine试图获取一个不存在的结果的member_id。
$matchresults->stek = $this->getRequest()->getPost('Res_Stek');
$matchresults->member_id = $this->getRequest()->getPost('Res_Name');
$matchresults->weight = $this->getRequest()->getPost('Res_Gewicht');
$matchresults->points = $this->getRequest()->getPost('Res_Punten');
$matchresults->match_id = $match_id;
$matchresults->amount = $this->getRequest()->getPost('Res_Aantal');
此代码确实将所有值正确地设置到实体中,但实体member_id字段只是被覆盖。 我该如何解决这个问题? 我可以创建一个不包含麻烦线的单独文件,但我认为这不是一个非常优雅的解决方案。
标记