开发者

Strange doctrine behavior on update

I have a simple table like following:

class SnookerCurrentInfo extends Doctrine_Record 
{
  public function setTableDefinition() 
  {
    $this->hasColumn('current_frame_id', 'integer', 4, array('notnull' => 'false'));
    $this->hasColumn('current_player_id', 'integer', 4, array('notnull' => 'false'));
    $this->hasColumn('current_score1', 'integer', 4, array('notnull' => 'false'));
    $this->hasColumn('current_score2', 'integer', 4, array('notnull' => 'false'));
  }

  public function setUp()
  {
    $this->setTableName('snooker_current_info');
  }
}

and I would like to keep only one entry in this table. So every time the value is changed I read the entry with id = 1 out and change the object and execute save. like the following:

$info = Doctr开发者_开发百科ine::getTable('SnookerCurrentInfo')->find(1);

$info->current_frame_id = $jsonInfo['current_frame_id'];
$info->current_player_id = $jsonInfo['current_player_id'];
$info->current_score1 = $jsonInfo['current_score1'];
$info->current_score2 = $jsonInfo['current_score2'];

$info->save();

but the strange thing is, I try to make it clear. Let's say at first, the entry is (30, 1, 1, 0) and I switch player, so update the entry to (30, 2, 1, 0). and I switch the player back again, so the entry should be updated to (30, 1, 1, 0), but this is not affected to the database!! In the database, the entry still remains as (30, 2, 1, 0)!!!!

But if after (30, 2, 1, 0), I update the score to (30, 2, 1, 1) and then switch the player back (30, 1, 1, 1) then this is ok.

What's that? How should I deal with it?


I realize this question is quite old, but maybe this will help someone else.

Doctrine 1.x (possibly 2.x as well) appears to having some issues with boolean values when checking for dirty fields. It's been a while since I worked with 1.x, but trying to updating a value from (int) 1 to 0 wouldn't work, it wouldn't recognize the changes (something about true/false is my guess).

What I ended up doing is replacing every (int) 0 with (string) 0 before persisting the changes.


Have you tried using update instead of the save?

Another way that works just as a save but might be more what you are needing is

 $info->replace();

instead of save();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜