0

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

对不起,我的英语不好,但我需要帮助。谢谢 :)

4

1 回答 1

1

你有 id_user 作为一个'integerOnly'=>true, 'on'=>'search',尝试将以下规则添加到你的模型中,这将强制 id_user 在所有情况下都是数字,而不仅仅是“搜索”:

array('id_user', 'numerical', 'integerOnly'=>true),
于 2012-08-13T07:48:20.193 回答