用户每次创建新用户时都会重复,echo $retMessage = "userID already exist.";
总是来帮助我们,请提前谢谢
用户::模型
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('userID', 'required'),
// array('userID', 'unique'),
array('year, cellphone', 'numerical', 'integerOnly'=>true),
array('userID', 'length', 'max'=>15),
array('password, lastname, firstname, middlename', 'length', 'max'=>45),
array('course', 'length', 'max'=>10),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('userID, password, lastname, firstname, middlename, course, year, cellphone', '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(
'Authitem'=>array(self::HAS_MANY, 'Authitem', array('name' => 'id')),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'userID' => 'User',
'password' => 'Password',
'lastname' => 'Lastname',
'firstname' => 'Firstname',
'middlename' => 'Middlename',
'course' => 'Course',
'year' => 'Year',
'cellphone' => 'Cellphone',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('userID',$this->userID,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('lastname',$this->lastname,true);
$criteria->compare('firstname',$this->firstname,true);
$criteria->compare('middlename',$this->middlename,true);
$criteria->compare('course',$this->course,true);
$criteria->compare('year',$this->year);
$criteria->compare('cellphone',$this->cellphone);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Users the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
维护::控制器
public function actionStudentmaintenance()
{
$model=new Users('search');
$this->render('studentmaintenance',array(
'model'=>$model,
));
}
public function actionCreateStudent()
{
$retVal="";
$retMessage="";
$model = new Users('search');
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
$model->role = $_POST['Users']['role'];
if($model->userID != '' && $model->password != '' && $model->role != '')
{
$check_user = Users::model()->findByAttributes(array('userID'=>$model->userID));
if(isset($check_user))
{
echo $retMessage = "userID already exist.";
die;
}
else
{
if($model->save())
{
$new_role = new Authassignment();
{
$new_role->itemname = $model->role;
$new_role->userID = $model->id;
if($new_role->save())
{
$retVal = "alert-success";
$retMessage = "Successfully Saved.";
}
}
}
}
}
else
{
echo $retVal = "alert-error";
echo $retMessage = "Please fill up the form.";
}
}
$model->unsetAttributes();
$this->render('studentmaintenance',array(
'model'=>$model,
));
}
答案 0 :(得分:0)
您好,因为您将UserID
定义为primary key
,但忘记将其定义为Autoincrement
,或者请展示您的db structure