我有一个看起来有点难以描述的问题,但无论如何我都会尝试。
在我的 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,因为教义试图获取一个不存在结果的 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 字段仅被覆盖。我将如何解决这个问题?我可以制作一个不包含令人不安的行的单独文件,但我认为这不是一个非常优雅的解决方案。
标记