Search Yii2在模型搜索()中修改find()方法
我试图在模型搜索中修改find()方法,它抛出一个错误“必须设置数据提供程序属性” 以下是我的搜索模型:Search Yii2在模型搜索()中修改find()方法,search,activerecord,model,yii2,Search,Activerecord,Model,Yii2,我试图在模型搜索中修改find()方法,它抛出一个错误“必须设置数据提供程序属性” 以下是我的搜索模型: public function search($params) { $userID = Yii::$app->user->identity->id; $groups = GroupAccess::find() ->where(['user_id' => $userID, 'item_name' => 'group_creator
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;
}
下面是我的控制器操作:
$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,
]);
}
}
优化查询很重要,因为用户应该能够看到其他组
如何正确修改find()方法
谢谢。我在这里看到两个错误:
所以子查询不需要foreach循环,对吗?此外,idAccesQuery可能返回多个id。这是组::find'id'=>array()的问题吗?谢谢对的这将向sql中添加一个子查询,并从GroupAccess db表中选择多个组id。请注意,在一个查询中不会有额外的请求do db-all-in-one查询。是否还有两个db查询,一个用于访问表,另一个用于组?请原谅,但是如何只有一个db操作。有两个find()方法。它是这样工作的:find方法只返回ActiveQuery对象,允许您从中获取普通sql,但在您调用其“fetch”方法(“one()”、“all()”、“column()”等)之前,它不会发出请求此处仅从dataProvider调用all()'方法。因此,在此之前,您可以使用许多ActiveQuery对象,将它们连接到第一个对象,并且最终只有一个sql请求。