dongxi1943 2016-10-27 20:47
浏览 63
已采纳

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

I've used the Gii AJAX Crud generator, and I'm being driven up a wall by my own stupidity. I am using Yii 2 and want to search with many to many, on a table that has that relation with ITSELF in a junction table, with the Grid View.

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']);
}

And in my grid-view /columns:

    [
    '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;
}

I'm able to display the results in my index table with that value function, but my Tag filter isn't able to search by tagTypes. How do I populate that?

As an example, when it's not many to many, I can use set my attribute to 'joinedTableName.value' and it works as soon as I add a $query->orFilterWhere('like', 'parent.name', $this->id) or whatever. But I'm at a loss now...

  • 写回答

1条回答 默认 最新

  • doucu7330 2016-10-29 13:31
    关注

    Declare $searchModel = new TagQuery() in your controller, then pass the $searchModel to the view and include it in the GridView options as 'filterModel' => $searchModel.

    Either that, or you can do really custom filters using specific filterTypes and filter logic for each column.

    You declare public tagType in the query model, but you don't do anything with it. $query->andFilterWhere(['like', 'tag.name', $this->tagType]);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab