Activerecord 是的,CDbTestCase save()失败

Activerecord 是的,CDbTestCase save()失败,activerecord,yii,phpunit,Activerecord,Yii,Phpunit,我想在我的应用程序中测试组的创建。所以我复制了教程中的AR测试,并根据我的需要进行了修改。我添加了一个fixture,如果我调用…::model()->findAll(),我会得到fixture的组 在我的测试中,我想创建一个新组,并验证该组是否已插入$此->assertTrue($group->save())通过,但var\u dump($group->id)为空。 以下是我的测试: public function testCreateGroup(){ // Insert new gr

我想在我的应用程序中测试组的创建。所以我复制了教程中的AR测试,并根据我的需要进行了修改。我添加了一个fixture,如果我调用…::model()->findAll(),我会得到fixture的组

在我的测试中,我想创建一个新组,并验证该组是否已插入$此->assertTrue($group->save())通过,但var\u dump($group->id)为空。 以下是我的测试:

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'));
}

}

我的臭虫在哪里

您不应使用
$group=new group::model()
创建新组,而应使用
$group=new group
创建新组

你确定你的列在数据库结构中被标记为自动递增吗?是的,这是一个MySQL表,列id是自动递增的,这很奇怪。你的RightGroup模型呢。它是生成的模型还是您编辑了它?如果是,请把它贴出来。奇怪的东西。你确定人工智能吗?你仔细检查了吗?你的数据库里有触发器吗?