Created
April 5, 2012 17:56
-
-
Save jazahn/2312855 to your computer and use it in GitHub Desktop.
getLastInsertId workaround for Oracle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class QActiveRecord extends CActiveRecord { | |
public function afterSave(){ | |
if(Yii::app()->db->driverName == 'oci'){ | |
try { | |
$connection=Yii::app()->db; | |
$sql = "select {$this->sequenceName}.currval from dual"; | |
$result = $connection->createCommand($sql)->queryRow(); | |
$this->ID = $result['CURRVAL']; | |
} catch (Exception $e) { | |
echo("Error: $e\n"); | |
} | |
} else { | |
// not implemented yet, doubt this will work | |
return parent::getLastInsertId(); | |
} | |
} | |
} | |
?> |
yes, this needs to be wrapped in a transaction
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not so sure about this solution. What happens if a transaction slips in between the SELECT SEQ.NEXTVAL and this select CURRVAL? Then you'll have the wrong ID.