Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
getLastInsertId workaround for Oracle
class QActiveRecord extends CActiveRecord {
public function afterSave(){
if(Yii::app()->db->driverName == 'oci'){
try {
$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();

This comment has been minimized.

Copy link

commented Oct 31, 2013

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.


This comment has been minimized.

Copy link
Owner Author

commented Jan 23, 2014

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
You can’t perform that action at this time.