du2229 2011-12-01 11:16
浏览 41

Symfony2和Doctrine2:Command循环在每个循环中创建新实体

I have a Symfony2 CLI script running which should update a timestamp each 3 seconds. The problem is: $this->em->persist($processInfo) creates a new entry in the DB each time it is executed. I want it to UPDATE, not to CREATE on each cycle.

$processInfo = new ....ProcessInfo();

while(true){
     $someObject = new ..();
     $this->em()->persist($someObject);

     $this->em()->clear(); // sorry forgot this line in my initial question

     $processInfo->setLastCheckOn($now); // to know if the script is still running, we set a timestamp in the db
     $this->em()->persist($processInfo);
     $this->em()->flush();


     sleep(3);
}

any ideas?

  • 写回答

1条回答 默认 最新

  • douchuang4402 2011-12-01 11:58
    关注

    Use persist only once. Then you need to commit the active transaction and start a new , in order to tell the database to save them and make a new transaction:

    $processInfo = new ....ProcessInfo();
    $this->em()->persist($processInfo);
    $this->em()->getConnection()->commit();
    while(true){
        $processInfo->setLastCheckOn($now); // to know if the script is still running, we set a timestamp in the db
        $this->em()->flush();
        sleep(3);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大