我有这个函数 duplicateCourseAction 其目标是......复制一个 Course 对象
public function duplicateCourseAction(Request $request) {
if ($this->getRequest()->isXmlHttpRequest() == false) {
return new Response("Bad request", 405);
}
$em = $this->getDoctrine()->getManager();
$parameters = $request->request->all();
$course = $em->getRepository('EntTimeBundle:Course')->findOneById($parameters['id']);
$duplicate = clone $course;
$duplicate->setDate(new \DateTime($parameters['date']));
$em->persist($duplicate);
$em->flush();
return new Response("200");
}
根据文档,“克隆”关键字制作表面副本(即参考副本)。这显然不是我想要的,因为我的 Course 实体包含与其他实体的许多关系,我宁愿想要一个值副本。
我发现了 unserialize(serialize(object)) 技巧:
public function duplicateCourseAction(Request $request) {
if ($this->getRequest()->isXmlHttpRequest() == false) {
return new Response("Bad request", 405);
}
$em = $this->getDoctrine()->getManager();
$parameters = $request->request->all();
$course = $em->getRepository('EntTimeBundle:Course')->findOneById($parameters['id']);
$duplicate = unserialize(serialize($course));
$duplicate->setDate(new \DateTime($parameters['date']));
$em->persist($duplicate);
$em->flush();
return new Response("200");
}
但是我对 Doctrine 有这个错误:
注意:未定义索引:/home/mart_q/Diderot/ent/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php 行 2776 中的 000000003ed2e9ea00000000ee270fde