Yii2修改find​​()方法的模型搜索方法() [英] Yii2 Modify find() Method in Model search()

查看:325
本文介绍了Yii2修改find​​()方法的模型搜索方法()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改find​​()方法的模型里面搜索方法,它抛出数据提供者属性必须设置为错误。

I am trying to modify the find() method inside the model search and it throws an error "The data provider property must be set".

下面是我的搜索模式:

public function search($params)
{

    $userID = Yii::$app->user->identity->id;

    $groups = GroupAccess::find()
    ->where(['user_id' => $userID, 'item_name' => 'group_creator'])
        ->asArray()
        ->all();
        foreach ($groups as $group) {
            $accessGroups[] = $group['group_id'];
        }

    $query = Group::find($accessGroups);

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

    $this->load($params);

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

    $query->andFilterWhere([
        'id' => $this->id,
        'status_id' => $this->status_id,
        //'created_user_id' => $this->created_user_id,
        'created_date' => $this->created_date,
        'profile_updated_user_id' => $this->profile_updated_user_id,
        'profile_updated_date' => $this->profile_updated_date,
        'last_accessed_user_id' => $this->last_accessed_user_id,
        'last_accessed_date' => $this->last_accessed_date,
    ]);

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

    return $dataProvider;
}

和这里是我的控制器操作:

And here is my controller action:

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

if (Yii::$app->request->isPjax) {
        return $this->renderAjax('groups', [
        'searchModel' => $searchModel,
        'dataProviderMine' => $dataProvider,
    ]);
    } else {
        return $this->render('groups', [
        'searchModel' => $searchModel,
        'dataProviderMine' => $dataProvider,
    ]);
    }

}

细化作为查询用户应该能够看到其它基团是很重要的。

It is important to refine the query as the user should be able to see other groups.

我怎样才能修改find​​()方法是否正确?

How can i modify the find() method properly?

感谢。

推荐答案

我看到两个错误的位置:

I see two bugs here:

  1. 您查找方法

  1. Your find method

$query = Group::find($accessGroups)

将无法正常工作 - 只是用

will not work - just replace it with

$query = Group::find()->where(['id' => $accessGroups]);

  • 我猜的数据提供程序属性必须设置为错误是由您的看法code。例如。如果您使用的是GridView控件,您应该设置它的'dataProvider中'插件选项:

  • I guess "The data provider property must be set" error is caused by your view code. E.g. if you are using GridView, you should set its 'dataProvider' widget option:

    GridView::widget([
        'dataProvider' => $dataProviderMine,
        'searchModel' => $searchModel,
        'columns' => [
            'id', 'status_id', 'created_date' // your view columns here
        ]
    ])

  • 也考虑使用子查询在您的搜索方式:

  • Consider also using sub queries in your search method:

    $idAccessQuery = GroupAccess::find()
        ->where(['user_id' => $userID, 'item_name' => 'group_creator'])
        ->select('group_id');
    $query = Group::find()->where([
        'id' => $idAccessQuery
    ]);

  • 这篇关于Yii2修改find​​()方法的模型搜索方法()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆