Question

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 ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top