【问题标题】:Yii, CDbTestCase save() failsYii,CDbTestCase save() 失败
【发布时间】:2013-06-10 19:29:44
【问题描述】:

我想在我的应用程序中测试组的创建。所以我复制了教程的 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'));
}

}

我的错误在哪里?

【问题讨论】:

  • 您确定您的列在您的数据库结构中被标记为自动增量吗?
  • 是的,它是一个 MySQL 表,列 id 是 Auto_Increment
  • 这很奇怪。你的 RightGroup 模型呢?它是生成的模型还是您对其进行了编辑?如果有,请发布。
  • 奇怪的东西。你确定人工智能?你仔细检查了吗?您的数据库中有任何触发器吗?

标签: activerecord yii phpunit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 2018-03-31
    • 1970-01-01
    • 2017-05-13
    • 2016-04-16
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    相关资源
    最近更新 更多