在yii2中搜索不使用单独的搜索模型

时间:2017-01-14 14:55:03

标签: search yii2

我是yii2的新手。我使用gii创建了模型,单独的搜索模型,视图和控制器。我的索引表单中有搜索但它无法正常工作。 我应该修改由gii生成的代码以使其正常工作吗?

我的索引视图代码如下:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'user_name',
        'eng_full_name',
        'phone',
        'mobile',
        'email:email',
        [
            'label' => 'Status',
            'attribute' => 'status',
            'value' => 'userStatus.cv_lbl',
        ],

        [
            'class' => 'yii\grid\ActionColumn',
            'contentOptions' => [
                'style' => 'width:80px;',
            ],
        ],
    ],
]) ?>

我的控制器:

public function actionIndex()
{
    $searchModel = new UserSearch();
    $dataProvider = $searchModel->search([Yii::$app->request->queryParams]);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

我的搜索模型是:

public function rules()
{
    return [
        [['user_id', 'br_id', 'role_id', 'designation', 'created_by', 'updated_by', 'update_count', 'status', 'type'], 'integer'],
        [['user_name'],'string'],
        [['user_name', 'user_password', 'eng_full_name', 'nep_full_name', 'phone', 'mobile', 'email', 'remarks', 'created_dt', 'updated_dt'], 'safe'],
    ];
}

public function search($params)
{
    $query = UserAccounts::find();
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => array('pageSize' => 50),
    ]);

    if (!($this->load($params) && $this->validate())) {
       return $dataProvider;
    }

    $query->andFilterWhere([
        'designation' => $this->designation,
        'status' => $this->status,
        'type' => $this->type,
    ]);

    $query->andFilterWhere(['like', 'user_name', $this->user_name])
        ->andFilterWhere(['like', 'user_password', $this->user_password])
        ->andFilterWhere(['like', 'eng_full_name', $this->eng_full_name])
        ->andFilterWhere(['like', 'phone', $this->phone])
        ->andFilterWhere(['like', 'mobile', $this->mobile])
        ->andFilterWhere(['like', 'email', $this->email])
        ->andFilterWhere(['like', 'remarks', $this->remarks]);

    return $dataProvider;
}

我的userAccounts型号:

class UserAccounts extends ActiveRecord implements IdentityInterface
{
    public $username;
    public $user_password_repeat;
    public $old_password;

    public static function tableName()
    {
        return 'user_accounts';
    }

    public function rules()
    {
        return [
            [['user_name', 'user_password',  'role_id', 'eng_full_name',  'designation',   'status', 'user_password_repeat'], 'required'],
            [['br_id', 'role_id', 'designation', 'created_by', 'updated_by', 'update_count', 'status', 'type'], 'integer'],
            [['created_dt', 'updated_dt','user_password_repeat'], 'safe'],
            [['user_name', 'user_password', 'eng_full_name',  'email'], 'string', 'max' => 50],
            [['phone', 'mobile'], 'string', 'max' => 10],
            [['remarks'], 'string', 'max' => 500],
            [['user_name'], 'unique'],
            [['user_password'], 'compare'],
            ['old_password', 'required' ,'on' => 'changePassword'],
            ['old_password', 'compareCurrentPassword', 'on' => 'changePassword'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'user_id' => 'User ID',
            'user_name' => 'user name',
            'user_password' => 'password',
            'user_password_repeat' =>'Confirm Password',
            'br_id' => 'Br ID',
            'role_id' => 'role id',
            'eng_full_name' => 'eng name',
            'phone' => 'phone',
            'mobile' => 'mobile',
            'designation' => 'designation',
            'email' => 'email',
            'remarks' => 'remarks',
            'created_by' => 'Created By',
            'created_dt' => 'Created Dt',
            'updated_by' => 'Updated By',
            'updated_dt' => 'Updated Dt',
            'update_count' => 'Update Count',
            'status' => 'Status',
            'type' => 'type',
            'old_password'=>'old password'
        ];
    }
}

2 个答案:

答案 0 :(得分:1)

您正在控制器中错误地发送搜索参数:

$dataProvider = $searchModel->search([Yii::$app->request->queryParams]);

您需要在没有方括号的情况下发送它们:

$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

答案 1 :(得分:0)

好的,我找到了我的问题的答案。有时项目中包含的jquery文件可能会导致此类问题.jquery文件之间可能存在冲突。在我的情况下,我已经包含了jquery-2.2.3.min.js。当我删除文件时搜索工作正常。