symfony 1.2.12 Propel
PHP 5.3.6
エラーログ
Cannot insert a value for auto-increment primary key (fuge.ID)
訳:自動インクリメントの主キー(fuge.ID)の値を挿入することはできません
問題箇所
lib/model/Fuge.php
public function updateNumber($updateNum)
{
return $this
->setNumber($updateNum)
->save();
}
特に挿入は行っていない。。
updateのはずなのにinsertを行っていますね
saveで行っている処理を確認
lib/model/om/BaseFuge.php
public function save(PropelPDO $con = null)
{
~~~略~~~
$con->beginTransaction();
try {
$affectedRows = $this->doSave($con);
$con->commit();
foreach (sfMixer::getCallables('BaseFuge:save:post') as $callable)
{
call_user_func($callable, $this, $con, $affectedRows);
}
FugePeer::addInstanceToPool($this);
return $affectedRows;
} catch (PropelException $e) {
$con->rollBack();
throw $e;
}
~~~略~~~
}
saveの中ではinsert,updateは区別してないくさい
transactionしてcommitしてる
中のdoSaveを見てみる
lib/model/om/BaseFuge.php
protected function doSave(PropelPDO $con)
{
~~~略~~~
if ($this->isNew() ) {
$this->modifiedColumns[] = FugePeer::ID;
}
// If this object has been modified, then save it to the database.
if ($this->isModified()) {
if ($this->isNew()) {
$pk = FugePeer::doInsert($this, $con);
$affectedRows += 1; // we are assuming that there is only 1 row per doInsert() which
// should always be true here (even though technically
// BasePeer::doInsert() can insert multiple rows).
$this->setId($pk); //[IMV] update autoincrement primary key
$this->setNew(false);
} else {
$affectedRows += FugePeer::doUpdate($this, $con);
}
$this->resetModified(); // [HL] After being saved an object is no longer 'modified'
}
~~~略~~~
}
$this->isNew()で FugePeer::doInsert
FugePeer::doUpdate
を区別している
isNewがちゃんと機能していない?
isNewはどこにあるのか
/symfony/plugins/sfPropelPlugin/lib/vendor/propel/om/BaseObject.php
private $_new = true;
~~~略~~~
public function isNew()
{
return $this->_new;
}
へー、標準trueだからどっかでfalseに変えてるのかな?
というかisNewオーバーライドしてた。。
lib/model/Fuge.php
/**
* @return bool
*/
public function isNew()
{
return (strtotime($this->getStartAt() . '+' . sfConfig::get('app_fuge_newAt') . ' day') >= strtotime('now'));
}
オーバーライドしたisNewをisNewStartAtなんなりにして解決
ある程度使っているclassで宣言しているものは把握しておくべきですね。