【发布时间】:2017-03-07 14:07:14
【问题描述】:
我已经为我的应用程序设置了一个日记服务。想法是将服务注入我的控制器(通过工厂),然后根据需要调用 diaryEntry 方法将日记条目存储到 dB 中。我遇到的问题是,如果我在控制器操作中多次调用该方法,我只会获得存储在 dB 中的最后一个条目。这是 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;
}
在控制器动作中,我有这样的东西:
$this->diaryService->diaryEntry($order->getProjectUrn(), 'test msg 1);
$this->diaryService->diaryEntry($order->getProjectUrn(), 'test msg 2);
问题是只有 msg 2 被保存在 dB 中,msg 1 没有被保存。最后一次更新是在 diaryEntry 方法的末尾添加刷新,但这并没有什么不同。有没有人有什么想法可以让我开始寻求解决这个问题?它是糟糕的架构,可能不适合作为服务,应该使用其他东西吗?
我很乐意粘贴应用程序所需的任何其他代码
提前感谢您的帮助
【问题讨论】:
标签: php doctrine-orm zend-framework2