我有 coba2 的控制器。在函数 actionAdmin 我将在搜索中添加验证,因为当整数被非整数搜索时,它会给出 SQL 错误
CDbCommand failed to execute the SQL statement: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "a" LINE 1: SELECT COUNT(*) FROM "yii_user" "t" WHERE id_user='a' ^. The SQL statement executed was: SELECT COUNT(*) FROM "yii_user" "t" WHERE id_user=:ycp0
这是我在模型上调用验证函数的函数
public function actionAdmin()
{
$model=new coba2('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['coba2']))
$model->attributes=$_GET['coba2'];
if($model->validate()){
$this->render('admin',array(
'model'=>$model,
));
} else{
$model->unsetAttributes();
$this->render('admin',array(
'model'=>$model,
));
}
}
这是我模型中的规则
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password, salt, date_create, date_update, date_birth', 'required'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id_user', 'numerical', 'integerOnly'=>true, 'on'=>'search'),
array('id_user, username, password, salt, date_create, date_update, date_birth', 'safe', 'on'=>'search'),
);
}
对不起,我的英语不好,但我需要帮助。谢谢 :)