ZF 1.9.5
这里。有人建议ON DUPLICATE KEY UPDATE
在使用 Zend_Db_Table 时捕获异常以进行模拟。
目前,我得到
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'i7dd30253497cfc0539d7c5830a926f7d' for key 'ukey'
..使用时
$orderRow = $this->createRow();
$orderRow->ukey = $ukey;
$orderRow->save();
所以,我想用try / catch
. 例外update
,否则insert
。
但我不知道该抓什么。Zend_Db_Exception
? PDOException
? Zend_Db_Adapter_Exception
? 我已经尝试了几个,但我认为我没有得到它。
稍后编辑。这对我有用:
try {
$orderRow = $this->createRow();
$orderRow->ukey = $ukey;
$orderRow->$stepCol = time();
$orderRow->save();
} catch (Zend_Db_Statement_Exception $e) {
// on UNIQUE error, update
if ($e->getCode() == 23000) {
$orderRow = $this->fetchRow($this->select()->where('ukey = ?', $ukey));
$orderRow->$stepCol = time();
$orderRow->save();
}
}