After saving ActiveRecord getPrimaryKey() returns an empty string. Why this can be? Database: mssql 2008
After saving ActiveRecord getPrimaryKey() returns an empty string. Why this can be? Database: mssql 2008
Well, I never used Yii with MSSQL, but first step in debugging would be to check your schema, to see if primary key really exist in your schema definitions.
Next step, would be to check primary key that is saved with your row(if you setup primary key correctly, db shouldn’t allow empty value for primary key).
Yes, primary key exists in my schema definitions. Primary key appointed automatically and I think after saving it must be not empty.
Can you post your code to see how you save your model and how you call the getPrimaryKey() ?
I created a test table and a test controller.
active record code
<?php
/**
* This is the model class for table "TestTable".
*
* The followings are the available columns in table 'TestTable':
* @property integer $id
* @property string $name
*/
class TestTable extends BaseAddressModel
{
/**
* Returns the static model of the specified AR class.
* @return TestTable the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'TestTable';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name', 'required'),
array('name', 'length', 'max'=>50),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
Controller code
<?php
class TestController extends CController
{
public function actionIndex()
{
$tt = new TestTable;
$tt->attributes = array('name' => 'qweasd');
$save_res = $tt->save();
Yii::log(var_export($save_res, true), 'info');
Yii::log(var_export($tt->getErrors(), true), 'info');
Yii::log(var_export($tt->getPrimaryKey(), true), 'info');
echo 'qweasd';
}
// Uncomment the following methods and override them if needed
/*
public function filters()
{
// return the filter configuration for this controller, e.g.:
return array(
'inlineFilterName',
array(
'class'=>'path.to.FilterClass',
'propertyName'=>'propertyValue',
),
);
}
public function actions()
{
// return external action classes, e.g.:
return array(
'action1'=>'path.to.ActionClass',
'action2'=>array(
'class'=>'path.to.AnotherActionClass',
'propertyName'=>'propertyValue',
),
);
}
*/
}
And in log file I see this
2011/07/19 14:46:58 [info] [application] true
in V:\home\compays_v2\protected\controllers\TestController.php (10)
in V:\home\compays_v2\index.php (13)
2011/07/19 14:46:58 [info] [application] array (
)
in V:\home\compays_v2\protected\controllers\TestController.php (11)
in V:\home\compays_v2\index.php (13)
2011/07/19 14:46:58 [info] [application] ''
in V:\home\compays_v2\protected\controllers\TestController.php (12)
in V:\home\compays_v2\index.php (13)
Think that the Id column in TestTable doesn’t have autoincrement trigger. Check this.
It has. Records saved in the database. Just after saving primary key is empty.