dongyan7851 2017-03-07 14:07
浏览 30
已采纳

ZF2 Doctrine服务 - 多个电话

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. 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. Here is 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. The last update was to add the flush at the end of the diaryEntry method but this has not made a difference. 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

  • 写回答

1条回答 默认 最新

  • douyun4524 2017-03-07 14:53
    关注

    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.

    Try replace your line with this one: $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. Same goes with timestamp.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)