I’m running Yii 1.1.8 and SQL Server 2008, on Windows Server 2008 / IIS7 with PHP 5.3.8.
When saving a model via ActiveRecord, the getLastInsertID returns an empty value. Here’s the code I’ve written that would retrieve the ID, which is part of the model and runs as normal (as an Event after running $mdl->save()):
public function afterSave() {
$this->EntryID = Yii::app()->db->getLastInsertID();
return parent::afterSave();
}
For those who do not want to touch the framework code, there is a workaround. As far as I can see, you could just safely use class PDO as long as you are not using transactions. To achieve this I created my own DBConnection class like so:
class DBConnection extends CDbConnection
{
private $_attributes=array();
protected function createPdoInstance()
{
$pdoClass=$this->pdoClass;
//
// Removed code block from original CDbConnection.
// This way, $this->pdoClass will be used, which defaults to 'PDO'
//
return new $pdoClass($this->connectionString,$this->username,
$this->password,$this->_attributes);
}
}
Then, set this class in main configuration:
'db'=>array(
'class' => 'DbConnection',
// ...
),
Works for me (SQLSRV driver 2.0, SQL Server 2005 and 2008 RC2, from Yii 1.1.8; not using transactions!)
I’m having this exact issue currently and I’m running 1.1.13. The fix listed here is already in place I checked. But after you save a new model the redirect to the view always fails because nothing is available in the id. So it goes to id= nothing.
Anybody have any ideas why this would be happening?