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

时间:2011-12-01 11:16:34

标签: php symfony doctrine-orm command-line-interface

我运行了一个Symfony2 CLI脚本,它应该每3秒更新一次时间戳。 问题是:$this->em->persist($processInfo)每次执行时都会在数据库中创建一个新条目。我想要它更新,而不是在每个周期创建。

$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);
}

任何想法?

1 个答案:

答案 0 :(得分:0)

仅使用persist一次。然后你需要提交活动事务并启动一个新事务,以告诉数据库保存它们并进行新的事务:

$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);
}