yii 基于多关系链接的网格视图过滤

9gm1akwq  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(130)

我有三张表:
1.使用者
1.团队
1.团队成员
我可以创建一个grideviw并显示用户名等没有问题,但我想做的是,如果显示一个团队列表(团队表-团队的名称等),其中当前用户是一个成员(团队成员表-用户ID和团队ID)。
在Team members模型中,我有这个

// {{{ getUser
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id']);
    } // }}} 
    // {{{ getTeam
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTeam()
    {
        return $this->hasOne(Teams::class, ['id' => 'team_id']);
    } // }}} 
}

在团队模型中我有这个

// {{{ getTeamMembers
/**
 * @return \yii\db\ActiveQuery
 */
public function getTeamMembers()
{
    return $this->hasMany(TeamMembers::class, ['team_id' => 'id']);
} // }}}

团队成员搜索模型

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\teammembers;

/**
 * TeamMembersSearch represents the model behind the search form of `app\models\teammembers`.
 */
class TeamMembersSearch extends teammembers
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'team_id', 'created_at', 'order_no'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = teammembers::find();

        // add conditions that should always apply here

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

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'user_id' => $this->user_id,
            'team_id' => $this->team_id,
            'created_at' => $this->created_at,
            'order_no' => $this->order_no,
            'time_start' => $this->time_start,
            'time_end' => $this->time_end,
            'time_total' => $this->time_total,
            'time_moving' => $this->time_moving
        ]);

        return $dataProvider;
    }
}

网格视图:

<?= GridView::widget([ // {{{ 
        'dataProvider' => $dataProvider,
        //'as filterBehavior' => \thrieu\grid\FilterStateBehavior::class,
        //'filterModel' => $searchModel,
        'columns' => [

            'id',
            'name',
            'members',
            //'manager_id',
            //'manager',
                [
                    'attribute' => 'Team Manager',
                    'value' => 'manager.UseName',
                    ],

                    [
                        'format' => 'raw',
                        'value' => function($model, $key, $index, $column) {

                                return Html::a(
                                    '<span class="glyphicon glyphicon-eye"></span>&nbsp;'.'View Team',
                                    Url::to(['userprofile/view', 'id' => $model->id]), 
                                    [
                                        'id'=>'grid-custom-button',

                                        'data-pjax'=>true,

                                        'action'=>Url::to(['userprofile/view', 'id' => $model->id]),

                                        'class'=>'btn btn-primary btn-sm',

                                    ]

                                );
                        }

                    ],

            //'created_at',
            //'updated_at',
           /*  [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{delete}    {view}',
                'contentOptions' => ['class' => 'text-center'],
            ], */
        ],
    ]); /* }}} */?>
</div>

所述控制器


* @return string|\yii\web\Response

 */
public function actionTeams()
{
    $searchModel = new TeamsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

csbfibhn1#

你必须把这个添加到你的User模型中:

/**
 * Return all teams where this user is registered
 * @return \yii\db\ActiveQuery
 */
public function getMyTeams()
{
    return $this->hasMany(Teams::class, ['id' => 'team_id'])
           ->viaTable('team_member_table_name', ['user_id' => 'id']);
}

或类似于:

/**
 * Return all teams where this user is registered
 * @return \yii\db\ActiveQuery
 */
public function getMyTeamMembers()
{
    return $this->hasMany(TeamMembers::class, ['user_id' => 'id']);;
}

/**
 * Return all teams where this user is registered
 * @return \yii\db\ActiveQuery
 */
public function getMyTeams()
{
    return $this->hasMany(Teams::class, ['id' => 'team_id'])
           ->via('myTeamMembers');
}

之后,您可以获得当前用户的所有团队,如下所示:

$teams = $user->myTeams; //return array of Teams object

以下是相关文档:通过连接表建立关系

相关问题