ZF 1.9.5
here. Someone suggested catching exceptions to emulate ON DUPLICATE KEY UPDATE
when using Zend_Db_Table.
Currently, I'm getting
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'i7dd30253497cfc0539d7c5830a926f7d' for key 'ukey'
..when using
$orderRow = $this->createRow();
$orderRow->ukey = $ukey;
$orderRow->save();
So, I want to catch that bugger with try / catch
. On exception update
, else insert
.
Zend_Db_Exception
? PDOException
? Zend_Db_Adapter_Exception
? I've tried several, but I don't think I got it.
Later edit. This worked for me:
try {
$orderRow = $this->createRow();
$orderRow->ukey = $ukey;
$orderRow->$stepCol = time();
$orderRow->save();
} catch (Zend_Db_Statement_Exception $e) {
// on UNIQUE er开发者_StackOverflow中文版ror, update
if ($e->getCode() == 23000) {
$orderRow = $this->fetchRow($this->select()->where('ukey = ?', $ukey));
$orderRow->$stepCol = time();
$orderRow->save();
}
}
Just look what exception is getting thrown like this:
try {
// query
} catch (Exception $e) {
var_dump(get_class($e));
}
That should tell you what kind of exception you need to catch because "Exception" will catch every type of exception, be it a ZF exception or a PDO exception or something completely different
It will throw a Zend_Db_Statement_Exception
.
Regarding finding out what Exception is thrown, you could take a look at edorian's answer.
精彩评论