我想要建立一个与许多关系
A节课
class Interview {
/**
* @OneToMany(targetEntity="Question", mappedBy="question")
*/
private $questions;
public function __construct() {
$this->questions = new ArrayCollection();
}
public function __toString() {
return $this->id;
}
/**
* @return Collection|Question[]
*/
public function getQuestions() {
return $this->questions;
}
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
......
}另一个
class Question {
/**
* @ManyToOne(targetEntity="Interview", inversedBy="interview")
* @JoinColumn(name="interview_id", referencedColumnName="id")
*/
private $interview;
public function getInterview() {
return $this->interview;
}
public function setInterview(Interview $interview) {
$this->interview = $interview;
return $this;
}
/**
* @ORM\Column(type="integer")
* @ORM\Id
*/
private $interview_id;
......
}以及所有这些的主计长
if ($form->isSubmitted() && $form->isValid()) {
$interview = new Interview();
$question = new Question();
$em->persist($interview);
$question->setInterview($interview);
$question->setTitle($request->get('title'));
$em->persist($question);
$em->flush();
return $this->redirectToRoute('homepage');
}我收到一个错误:
类型为AppBundle\ Entity \问号的实体缺少为字段“interview_ ID”分配的id。该实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果希望自动生成标识符,则需要相应地调整元数据映射。
不知道问题出在哪里,怎么解决。
发布于 2018-04-01 13:32:44
强制再次从数据库加载对象,而不是从标识映射为对象提供服务。您可以在执行$em->clear();后调用$em->persist($interview);,即
$interview = new Interview();
$em->persist($interview);
$em->clear();发布于 2018-04-01 18:05:22
发布于 2019-09-19 05:52:08
我确信,现在回答已经太晚了,但是也许其他人会收到这个错误:-D当链接实体(在这里,面试实体)为null时,您会得到这个错误。
当然,您已经实例化了Interview.But的一个新实例,因为这个实体只包含一个字段( id ),在这个实体被添加之前,它的id等于NULL。因为没有其他领域,所以教义认为这个实体是空的。在将该实体链接到另一个实体之前,您可以通过调用刷新()来解决这个问题。
https://stackoverflow.com/questions/49598334
复制相似问题