我想在我的应用程序中测试组的创建。所以我复制了教程的 AR-Test 并根据我的需要进行了修改。我添加了一个夹具,如果我调用 ...::model()->findAll() 我得到夹具的组。
在我的测试中,我想创建一个新组并验证该组是否已插入。$this->assertTrue($group->save()) 通过但 var_dump($group->id) 为 NULL。这是我的测试:
public function testCreateGroup(){
// Insert new group
$group= RightGroup::model();
$group->title = 'Create Test';
$this->assertTrue($group->save());
// Verify created and updated date
$group = RightGroup::model()->findByPk($group->id);
$this->assertTrue($group instanceof RightGroup);
}
这是我的模型。它是由我自己自动生成和编辑的。
class RightGroup extends CActiveRecord {
// Constants
const ADMIN_GROUP = 1;
const USER_GROUP = 2;
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return RightGroup 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 'right_groups';
}
/**
* @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('title', 'required'),
array('title', 'length', 'max' => 50),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, title, created, updated', 'safe', 'on' => 'search'),
// Set the updated value for each update
array('updated', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'update'),
// Set the created and updated values at create action
array('created,updated', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'insert')
);
}
/**
* @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(
'users' => array(self::MANY_MANY, 'User', 'right_group_users(group_id, user_id)'),
'rights' => array(self::MANY_MANY, 'Right', 'right_groups_rights(group_id, right_id)'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => Yii::t('right','ID'),
'title' => Yii::t('right','Title'),
'created' => Yii::t('right','Created'),
'updated' => Yii::t('right','Updated'),
);
}
/**
* 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, true);
$criteria->compare('title', $this->title, true);
$criteria->compare('created', $this->created, true);
$criteria->compare('updated', $this->updated, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
/**
* Proofs if the given right belongs to this right group.
*
* @param Right $right
* @return bool
*/
public function hasRight(Right $right) {
return in_array($right, $this->rights);
}
public function behaviors() {
return array('CAdvancedArBehavior' => array(
'class' => 'application.extensions.CAdvancedArBehavior'));
}
}
我的错误在哪里?