yii2添加搜索小部件网格视图

时间:2017-08-20 17:47:40

标签: yii2

**如何将girdView搜索添加到我的关系表列?

我有除了我的关系表之外的所有girlview搜索框 (developersActivity.developer_point)和('developersActivity.project_done') 我有他们的价值,但小部件搜索框不是他们上面的**

我的控制器

   $searchModel = new DeveloperSearch();

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

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

我的搜索模型

class DeveloperSearch extends Developers
{
    public function rules()
    {
        return parent::rules();
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    public function search($param)
    {

        $query = Developers::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query
        ]);

        $this->load($param);

        $query->joinWith('developersActivity');

        $dataProvider->setSort([
            'attributes'=> [
                'name',
                'developersActivity.developer_point'=>[
                    'asc'=>['developer_point'=>SORT_ASC],
                    'desc'=>['developer_point'=>SORT_DESC],
                ]
            ]
        ]);

        $query->andFilterWhere([
            'developer_id' => $this->developer_id,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'email', $this->email]);

        return $dataProvider;
    }
}

模型

class Developers extends ActiveRecord
{

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



    public function rules()
    {
        return [
            [['name',
                'family',
                'phone',
                'email',
                'address',
                'brithday',
                'age',
                'ability',
                'role',

                'join_date',
                ], 'required'],
            [['developer_id'], 'integer'],
            ['email','email'],
            [['phone'],'integer', 'min' => 10],
            [['address'], 'string', 'max' => 100],
            [['name'], 'string', 'min' => 3],

        ];
    }



    public  function getDevelopersActivity(){
        return $this->hasOne(DevelopersActivity::className(),['developer_activity_id' => 'developer_id']);
    }

}

和developersActivity模型类

class DevelopersActivity extends ActiveRecord
{
    public function rules()
    {
        return [
            [['developer_activity_id',
                'developer_point',
                'project_done',
                'free_rate',
                'address',
                'estimate_for_next_project',
                'date_project_done',
                'number_of_project',
                'activity_rate',
            ], 'safe'],

        ];

    }

}

这是视图     

    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,

    'columns' => [
        'developer_id',
        'name',
        'email',
        'phone',
        'email',
        'developersActivity.developer_point',
        'developersActivity.project_done'


        //  'value'=>'developersActivity.point',
        //'contentOptions'=>['style'=>'width: 120px;']
    ],


]);
?>

1 个答案:

答案 0 :(得分:2)

在模型中

为字段添加一个getter(假设该字段名为actvityname)

/* Getter for deleveloer activity name   */
 public function getDevelopersActivityName() {
   return $this->developersActivity->activityname;
 }
在您的searchModel

为相关字段添加公共变量,并在规则中声明为安全

  /* your calculated attribute */
  public $activityName;

  /* setup rules */
  public function rules() {
     return [
      /* your other rules */
      [['activityName'], 'safe']
     ];
  }

在过滤器中,您可以为字段添加过滤器

    // filter by developer activity
    $query->joinWith(['developersActivity' => function ($q) {
        $q->where('yor_develeoper_activity_table.your_developer_activity_column LIKE "%' . $this->activityName. '%"');
    }]);

    return $dataProvider;

在gridview中,您可以直接使用

进行参考
  'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    'id',
    'fullName',
    'activityName',
    ['class' => 'yii\grid\ActionColumn'],
]

您可以在本教程中查看一些建议http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/