Yii2 多对多与自我 - 通过网格视图过滤(没有属性?)

Yii2 Many to Many with Self - filter through grid view (no attribute?)

我已经使用了 Gii AJAX Crud 生成器,但我正被自己的愚蠢逼上墙。我正在使用 Yii 2 并希望在一个 table 上进行多对多搜索,该 table 与结点 table 中的 ITSELF 具有这种关系,并带有网格视图。

table tag (id, name).

table tag_child (parent_id, child_id)


Class Tag
...
public function getParents()
{
    return $this->hasMany(self::className(), ['id' => 'child_id'])
        ->viaTable('tag_child', ['parent_id' => 'id']);
}

public function getChildren()
{
    return $this->hasMany(self::className(), ['id' => 'parent_id'])
        ->viaTable('tag_child', ['child_id' => 'id']);
}

在我的网格视图/列中:

    [
    'class' => '\kartik\grid\DataColumn',
    'attribute'=>'name',
],
[
    'class' => '\kartik\grid\DataColumn',
    'label' => 'Tag Type',
    'value' => function($tag) {
        return $tag->displayTagTypes();
    },
    'attribute' => 'tagTypes'
],


 TagQuery.php
 ...
 public $tagTypes;
        public function search($params)
 {
    $query = Tag::find();

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

    $this->load($params);

    if (!$this->validate()) {
        // $query->where('0=1');
        return $dataProvider;
    }

    $query->joinWith('parents p');

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

    $query->andFilterWhere(['like', 'tag.name', $this->name]);

    return $dataProvider;
}

我可以使用该值函数在索引 table 中显示结果,但我的标签过滤器无​​法按标签类型进行搜索。我如何填充它?

例如,当它不是多对多时,我可以将我的属性设置为 'joinedTableName.value',只要我添加 $query->orFilterWhere('like', 'parent.name', $this->id) 或其他。但是我现在很茫然...

在您的控制器中声明 $searchModel = new TagQuery(),然后将 $searchModel 传递给视图并将其作为 'filterModel' => $searchModel.

包含在 GridView 选项中

或者,您可以使用特定的过滤器类型和每列的过滤器逻辑来真正自定义过滤器。

您在查询模型中声明了 public tagType,但您没有对其进行任何操作。 $query->andFilterWhere(['like', 'tag.name', $this->tagType]);