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

これが私のモデルでvalidate関数を呼び出すための私の関数です

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 に答える