문제

In my unit tests (on symfony 1.4), I was naively trying to edit a record to make my tests. However, I found out after some bug hunt that my script was not doing what I expected it to do. Here is the offending part:

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

=> my location id has not changed whatsoever ! Whenever I do a $tp_vehicle->getLocationId(), I get the old value which is 8 !

So I tried this way round

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

And this way:

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

With no luck ! In the end, I could get What I wanted with this piece of code:

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

Which is quite awkward to me, since I really do not understand the logic that is at work behind this. I kind of have the intuition that I am missing something related to cache and doctrine optimizations but... I fail to understand exactly how this is all put together.

Can anyone shed a light on this ?

도움이 되었습니까?

해결책

Doctrine permits to refresh records, as mentionned in the doc. This would go as follow:

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

See also this post on stack overflow.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top