与Doctrine更新混淆。 (1.2.4)

时间:2014-02-14 14:51:28

标签: doctrine-1.2

在我的单元测试中(在symfony 1.4上),我天真地试图编辑一条记录来进行测试。但是,我发现一些bug后我的脚本没有按照我的预期去做。这是令人不快的部分:

$tp_vehicle = Doctrine_Core::getTable("tpVehicle")->find(10);
$tp_vehicle->setLocationId(7);
$tp_vehicle->save();

=>我的位置ID没有任何改变!每当我做$tp_vehicle->getLocationId()时,我得到的旧值是8!

所以我尝试了这种方式

$tp_vehicle = Doctrine_Core::getTable("tpVehicle")->find(10);
$tp_vehicle->location_id = 7;
$tp_vehicle->save();

就这样:

Doctrine_Core::getTable("tpVehicle")->createQuery("v")
                            ->update()
                            ->set("location_id", 7)
                            ->where("id = ?", 10)
                            ->execute();

没有运气! 最后,我可以用这段代码得到我想要的东西:

$tp_vehicle->setLocation(Doctrine_Core::getTable("tpLocation")->find(7));

这对我来说非常尴尬,因为我真的不明白这背后的工作逻辑。我有点直觉,我遗漏了与缓存和学说优化有关的东西,但是......我无法理解这些是如何组合在一起的。

任何人都可以对此有所了解吗?

1 个答案:

答案 0 :(得分:0)

Doctrine允许刷新记录,如doc中所述。这将如下:

$tp_vehicle->refresh();
$tp_vehicle->getLocationId();

另请参阅有关堆栈溢出的this帖子。