Php 原则将给定值变为空值
我有一个问题看起来有点难以描述,但我还是要试试 在我的MatchesResultscontroller中,我有以下代码来构建实体:Php 原则将给定值变为空值,php,doctrine-orm,doctrine,Php,Doctrine Orm,Doctrine,我有一个问题看起来有点难以描述,但我还是要试试 在我的MatchesResultscontroller中,我有以下代码来构建实体: $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); if($this->getRequest()->isPost()) { // get id of current match $match_
$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;
}
}
此代码确实将所有值正确设置到实体中,但实体成员id字段只是被覆盖。
我将如何解决这个问题?
我可以制作一个不包含麻烦行的单独文件,但我认为这不是一个非常优雅的解决方案
标记您不应该在实体内使用外键引用。
原则的全部要点是允许您使用实际的对象实例(因此int$member\u id
变成member$member
)。你为什么用这种方式,有什么特别的原因吗?嗯,坦率地说,这是因为我没有创造出我现在必须使用的东西,我只需要改进它。现在,关于你的答案(原则的全部要点是允许你使用实际的对象实例(因此int$member\u id变成member$member)。),这会如何改变结果?它不会改变结果,而会改变编写它的人。
$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');