Skip to content

Instantly share code, notes, and snippets.

@jazahn
Created April 5, 2012 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jazahn/2312855 to your computer and use it in GitHub Desktop.
Save jazahn/2312855 to your computer and use it in GitHub Desktop.
getLastInsertId workaround for Oracle
<?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();
}
}
}
?>
@psbauman
Copy link

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.

@jazahn
Copy link
Author

jazahn 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