学说不做更新

时间:2013-05-31 06:17:10

标签: php mysql doctrine-orm flush

我正在开发一个使用Doctrine的PHP项目。问题是实体的更新没有经过,即数据库没有更新。代码如下所示:

// findOrGetUser does the lookup for the entity, and will create it if 
// it isn't found.  $userAccount is a Doctrine entity
$userAccount = findOrGetUser($user);

// setAuthTok is a member function of User, defined in DataObjs.php, 
// that sets a property of User
$userAccount->setAuthTok("hello");

$em->flush();

如果数据库中不存在该实体,则会创建一条记录,并将authTok设置为“hello”。如果没有,它将保留其先前的值(212位数字)。因此,更新将在新创建的记录上进行,但不会在更新方案中进行。

有谁知道发生了什么事?

数据库是MySQL,我尝试了mysqli和pdomysql驱动程序,他们都做了同样的事情。我尝试在刷新之前添加一个persist()调用,但这没有任何区别。

我尝试添加显式事务处理,我也查看了Doctrine源代码并尽可能地跟踪它,并且找不到任何错误。

编辑 - 我忘了提到这段代码在测试服务器上运行正常。所以似乎问题是某种配置错误?

以下是我在尝试显式事务处理时使用的代码:

try {
    $em->getConnection()->beginTransaction();
    $userAccount = findOrGetUser($user);
    $userAccount->setAuthTok("hello");
    $em->persist($userAccount);
    $em->flush();
    $em->getConnection()->commit();
} catch (Exception $ex) {
    fileLog("(" . $ex->getFile() . " " . $ex->getLine() . ") " .$ex->getMessage());
    $em->getConnection()->rollback();
    $em->close();
    throw $e;
}

1 个答案:

答案 0 :(得分:0)

我意识到你说你曾经尝试在冲洗之前添加一个对persist()的调用,但是你没有说你如何调用所以我会回答这个问题: 你需要拨打$ em-> persist($ userAccount);在刷新之前,否则实体管理器无需写入数据库。

相关问题