【发布时间】:2019-02-19 18:22:33
【问题描述】:
序言
我正在尝试发布(在 Postgresql 数据库中插入)一个 JSON 格式的实体,这要归功于带有 JMSSerializerBundle 的 FOSRestBundle 路由。该实体如下所示:
**Vote** : OneToOne Bidirectional : **Question** : OneToMany Bidirectional : Answer
这里的 JSON 有效负载:
{
"title": "string",
"description": "string",
"question": {
"id": 0,
"title": "string",
"description": "string",
"answers": [
{
"title": "string",
"description": "string"
},
{
"title": "First answer ?",
"description": "string"
}
]
}
}
问题
当它插入投票时,问题字段中的vote_id 为空,答案中的question_id 为空。
当我从路由中获取有效负载时,它会转换为带有fos_rest.request_body 的对象,操作如下:
public function postVoteAction(Vote $vote, ConstraintViolationList $violations)
{
if (count($violations)) {
return $this->view($violations, Response::HTTP_BAD_REQUEST);
}
$em = $this->getDoctrine()->getManager();
$vote->setOwner($this->getUser());
$em->persist($vote);
$em->flush();
return $vote;
}
我确实得到了一个带有我的问题和答案的 Vote 对象,但是当它被插入到数据库中时,正如前面所说的外键字段为 NULL。
我已经做了什么
我查看了关系并查看了实体 cascade={"persist"} 中是否存在持久性
// in vote
@ORM\OneToOne(targetEntity="Question", mappedBy="vote", cascade={"persist", "remove"})
private $question;
// in question
@ORM\OneToOne(targetEntity="Vote", inversedBy="question", cascade={"persist"})
@ORM\JoinColumn(name="vote_id", referencedColumnName="id")
private $vote;
@ORM\OneToMany(targetEntity="Answer", mappedBy="question", cascade={"persist", "remove"})
private $answers;
// in answer
@ORM\ManyToOne(targetEntity="Question", inversedBy="answers", cascade={"persist"})
@ORM\JoinColumn(name="question_id", referencedColumnName="id")
private $question;
我使用php bin\console make:entity --regenerate 获取所有
获取器/设置器。
我清除了数据库并重新生成了它。
回答
正如@yTko 所说,我忘记将引用放回我的控制器中的对象,我认为它是由 Doctrine 使用持久化创建的,所以现在这是我的工作代码:
public function postVoteAction(Vote $vote, ConstraintViolationList $violations)
{
if (count($violations)) {
return $this->view($violations, Response::HTTP_BAD_REQUEST);
}
$em = $this->getDoctrine()->getManager();
$vote->setOwner($this->getUser());
$question = $vote->getQuestion();
$question->setVote($vote);
foreach ($question->getAnswers() as $answer) {
$answer->setQuestion($question);
}
$em->persist($vote);
$em->flush();
return $vote;
}
【问题讨论】:
-
任何人都可以爬过很多代码。请将其缩减为 Minimal, Complete and Verifiable Example 并将代码包含在您的问题中。
-
请向我们展示定义实体之间关系的注释。
-
@Zak 添加了他们。
标签: php postgresql symfony doctrine-orm fosrestbundle