我有2个实体:提交和投票。
提交实体:
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Votes", mappedBy="submission",cascade={"persist", "remove" })
* @ORM\JoinColumn(name="id", referencedColumnName="submission_id")
*/
protected $vote;
/**
* @return mixed
*/
public function getVote()
{
return $this->vote->toArray();
}
/**
* @param Votes $vote
* @return $this
*
*/
public function setVote(Votes $vote)
{
if (!$this->vote->contains($vote)) {
$this->vote->add($vote);
}
return $this;
}
投票实体:
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Submission", inversedBy="id")
* @ORM\JoinColumn(name="submission_id", referencedColumnName="id", nullable=true)
*/
protected $submission;
/**
* @return mixed
*/
public function getSubmission()
{
return $this->submission;
}
/**
* @param mixed $submission
*/
public function setSubmission($submission)
{
$this->submission = $submission;
}
问题是,当我为之前选择的提交设置投票时:
$submission = $em->getRepository('AppBundle:Submission')->findOneById($submissionId);
$vote = new Votes();
$vote->setId($submission->getId());
$vote->setFeedback($judgeComment);
$vote->setScore($judgeScore);
$vote->setSubmission($submissionId);
$submission->setVote($vote);
$em->persist($submission);
$em->flush();
submission_id列总是为NULL - 我不确定我做错了什么。我想在这里存储提交ID,这是什么投票。
答案 0 :(得分:0)
您不必在教条中设置ID,而是设置对象,因为它是ORM
$submission = $em->getRepository('AppBundle:Submission')->findOneById($submissionId);
$vote = new Votes();
$vote->setId($submission->getId()); // <--- Are you sure that you need this?
$vote->setFeedback($judgeComment);
$vote->setScore($judgeScore);
// $vote->setSubmission($submissionId); <--- ERROR!
$vote->setSubmission($submission) // <--- Check advice below
$submission->setVote($vote);
$em->persist($submission);
$em->flush();
如果我也可以在此处提出建议,请按以下步骤修改setVote
public function setVote(Votes $vote)
{
if (!$this->vote->contains($vote)) {
$this->vote->add($vote);
$vote->setSubmission($this);
}
return $this;
}
并且您永远不需要明确设置关联的双方
我还注意到您的班级名称是复数( 应该是单数进入域名),而vote
属性为submission
是奇异的( 应该是复数,因为它是一个集合;你有一个-to-Many
注释。即使setter
也应该是adder
,因为你没有设置,只是添加一个Vote
)
答案 1 :(得分:0)
非常感谢格里。谢谢你,问题解决了。这是答案:
提交实体:
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Votes", mappedBy="submission",cascade={"persist", "remove" })
* @ORM\JoinColumn(name="id", referencedColumnName="submission_id")
*/
protected $vote;
/**
* @return mixed
*/
public function getVote()
{
return $this->vote->toArray();
}
/**
* @param Votes $vote
* @return $this
*
*/
public function setVote(Votes $vote)
{
$this->vote[] = $vote;
$vote->setSubmission($this);
return $this;
}
投票实体:
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Submission", inversedBy="vote")
* @ORM\JoinColumn(name="submission_id", referencedColumnName="id",onDelete="cascade", nullable=true)
*/
protected $submission;
/**
* @return mixed
*/
public function getSubmission()
{
return $this->submission;
}
/**
* @param mixed $submission
*/
public function setSubmission($submission)
{
$this->submission = $submission;
}
现在,当我添加新的投票时,一切都很好:
$vote = new Votes();
$vote->setFeedback($judgeComment);
$vote->setScore($judgeScore);
$submission->setVote($vote);
$em->persist($submission);
$em->flush();