Yii2修改Model search()中的find()方法
Yii2 Modify find() Method in Model search()
我正在尝试修改模型搜索中的 find() 方法,但它抛出错误 "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;
}
这是我的控制器操作:
$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() 方法?
谢谢。
我在这里看到两个错误:
您的查找方法
$query = Group::find($accessGroups)
将不起作用 - 只需将其替换为
$query = Group::find()->where(['id' => $accessGroups]);
我猜"The data provider property must be set"错误是由你的视图代码引起的。例如。如果您使用的是 GridView,则应设置其 'dataProvider' 小部件选项:
GridView::widget([
'dataProvider' => $dataProviderMine,
'searchModel' => $searchModel,
'columns' => [
'id', 'status_id', 'created_date' // your view columns here
]
])
考虑在搜索方法中也使用子查询:
$idAccessQuery = GroupAccess::find()
->where(['user_id' => $userID, 'item_name' => 'group_creator'])
->select('group_id');
$query = Group::find()->where([
'id' => $idAccessQuery
]);
我正在尝试修改模型搜索中的 find() 方法,但它抛出错误 "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;
}
这是我的控制器操作:
$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() 方法?
谢谢。
我在这里看到两个错误:
您的查找方法
$query = Group::find($accessGroups)
将不起作用 - 只需将其替换为
$query = Group::find()->where(['id' => $accessGroups]);
我猜"The data provider property must be set"错误是由你的视图代码引起的。例如。如果您使用的是 GridView,则应设置其 'dataProvider' 小部件选项:
GridView::widget([ 'dataProvider' => $dataProviderMine, 'searchModel' => $searchModel, 'columns' => [ 'id', 'status_id', 'created_date' // your view columns here ] ])
考虑在搜索方法中也使用子查询:
$idAccessQuery = GroupAccess::find() ->where(['user_id' => $userID, 'item_name' => 'group_creator']) ->select('group_id'); $query = Group::find()->where([ 'id' => $idAccessQuery ]);