LoginSignup
1
1

More than 5 years have passed since last update.

symfony1.2で更新処理時に「Cannot insert a value for auto-increment primary key」エラーが出る

Posted at

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で宣言しているものは把握しておくべきですね。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1