[英]ZF2 Doctrine Service - Multiple calls
I have setup a diary service for my application. 我已经为我的应用程序设置了日记服务。 The idea was to inject the service into my controllers (though a factory) and then call the diaryEntry method as required to store diary entries into the dB.
想法是将服务注入我的控制器(尽管是工厂)中,然后根据需要调用diaryEntry方法以将日记条目存储到dB中。 The issue I am having is I am only getting the last entry stored in the dB if I call the method more than once in a controller action.
我遇到的问题是,如果我在控制器操作中多次调用该方法,只会得到最后一个存储在dB中的条目。 Here is DiaryService::diaryEntry -
这是DiaryService :: diaryEntry-
public function diaryEntry($projectUrn, $msg, $diaryCategory = 14)
{
$em = $this->em;
$user = $this->user;
$ProjectDiary = $this->projectDiaryEntity;
if(is_numeric($projectUrn)) {
$projectUrn = $em->find('Application\Entity\ProjectUrn', $projectUrn);
}
$ProjectDiary->setProjectUrn($projectUrn);
$ProjectDiary->setDiaryCategory($em->find('Application\Entity\DiaryCategory', $diaryCategory));
$ProjectDiary->setStatus(1);
$ProjectDiary->setComment($msg);
$ProjectDiary->setUser($em->getRepository('Application\Entity\Users')->findOneBy(['username' => $user->username]));
$ProjectDiary->setTimestamp(new \DateTime());
$em->persist($ProjectDiary);
$em->flush();
return;
}
In a controller action I then have something like this: 在控制器操作中,我将得到以下内容:
$this->diaryService->diaryEntry($order->getProjectUrn(), 'test msg 1);
$this->diaryService->diaryEntry($order->getProjectUrn(), 'test msg 2);
The issue is only msg 2 is getting saved in the dB, msg 1 is not being saved. 问题是,仅味精2已保存在dB中,味精1未保存。 The last update was to add the flush at the end of the diaryEntry method but this has not made a difference.
最后的更新是在diaryEntry方法的末尾添加了刷新,但这并没有改变。 Does anyone have any ideas where I coudl start looking to solve this issue?
有谁对我可以解决这个问题有任何想法? Is it bad architecture and perhaps not suitable as a service and should utilise something else?
它是糟糕的体系结构,也许不适合作为服务,应该利用其他东西吗?
I am happy to paste any further code required from the app 我很乐意粘贴应用程序中所需的任何其他代码
Thank you in advance for any assistance 预先感谢您的协助
It would help more if you post information about how $this->projectDiaryEntity
is initialized, but I bet you inject diary entity object through factory, so all your operations are done on same object. 如果您发布有关如何初始化
$this->projectDiaryEntity
信息,这会有所帮助,但是我敢打赌,您会通过工厂注入日记实体对象,因此所有操作都在同一对象上完成。
Try replace your line with this one: $ProjectDiary = clone $this->projectDiaryEntity;
尝试用这一行替换您的行:
$ProjectDiary = clone $this->projectDiaryEntity;
Your method does not look good however it's hard to tell what you should change based on a few lines of one method. 您的方法看起来不太好,但是很难根据一种方法的几行来确定应该更改的内容。
If your default status
is 1
, you can set it in your entity class. 如果您的默认
status
为1
,则可以在您的实体类中进行设置。 Same goes with timestamp. 时间戳也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.