2013-06-14 3 views
0

Я хочу проверить создание группы в своем приложении. Поэтому я скопировал AR-тест учебника и внедрил его в соответствии с моими потребностями. Я добавил прибор и, если я позвоню ... :: model() -> findAll() Я получаю группы приборов.Yii, CDbTestCase save() failed

В моем тесте я хочу создать новую группу и подтвердить, что группа была вставлена. $ 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')); 
} 

} 

Где моя ошибка?

+0

Вы уверены, что ваша колонка отмечена как Auto Increment в вашей структуре db? – MrSoundless

+0

Да, это таблица MySQL, а идентификатор столбца Auto_Increment – EvilKarter

+0

Это странно. Как насчет вашей модели RightGroup. Это сгенерированная модель или вы ее отредактировали? Если да, отправьте его. – MrSoundless

ответ

1

Вы не должны нарушать новую группу с $group = new Group::model(), но $group = new Group.

+0

Омфг ... Я так глуп! Большое спасибо!!!! Это решило мою проблему! – EvilKarter

+0

Омг хороший улов, я даже не заметил, что: p – MrSoundless

Смежные вопросы