2017-01-14 9 views
0

私はyii2を初めて使っています。モデルを作成しました。検索モデル、ビュー、コントローラをgiiで作成しました。インデックスフォームに検索があり、動作しません。 giiで生成されたコードを修正して正常に動作させる必要がありますか?検索がyii2の別の検索モデルで動作しない

マイindexビューのコードは次のようである:

<?= 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' 
     ]; 
    } 
} 
+0

通常はgiiを使用して検索機能がすぐに利用できます。変更は必要ありません。作業していないとはどういう意味ですか?エラーなどありますか? ..あなたの問題をより良く説明してください – scaisEdge

+0

私の検索がwondering.searchでないのはなぜですか?名前、電話、メール、モバイルでは動作しません。またエラーもありません。 –

+0

質問を更新して追加してください関連するモデルコード – scaisEdge

答えて

1

はあなたのコントローラに誤った検索のparamsを送信している:私は私のquestion.Sometimes jqueryのファイルに対する答えを見つけることが

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

私はまだそれが動作していないと言ったように私は –

0

OK:

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

あなたは角括弧なしでそれらを送信する必要があります私の場合、jquery-2.2.3.min.js.whenを含んでいました。そのファイルを削除して検索が正常に動作しています。

関連する問題