I have append query in controller using dataprovider object but my last query is not apply after used getKeys() method.
Why without getKeys() method is possible but with getKeys() not possible?
Please view my below code.
$searchModel = new MySearchModel();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->sort = false;
$pKey = $dataProvider->getKeys();
$otherId = MyModel::find()->andWhere(['id' => $pKey])->select('id')->column();
//This query is not apply/append when i used `getKeys()` method
$dataProvider->query->andWhere(['id' => $otherId]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
Related
I have a query in Model Autos getSpecialItems() it is used multiple times in project, but i need add in controller to filter $dataProvider.
How make this whitout write same query again in controller?
Autos.php
public function getSpecialItems()
{
return self::find()->where(['id_category' => 18])->all();
}
controller.php
public function actionIndex()
{
$searchModel = new AutosSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
//need add query here
//$dataProvider->query->$searchModel->getSpecialItems();
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
You have to add filter in to your AutosSearch() model after validate():
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions you need
$query->andFilterWhere([
'id_category' => 18,
]);
I've this code
public function actionIndex() {
$model = new Out();
$searchModel = new VatoutFakturOutSearch();
if (Yii::$app->request->post('hasEditable')) {
$userId = Yii::$app->user->id;
$searchModel->user_id = $userId;
$searchModel->parent_id = $userId;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination->pageSize = 100;
return $this->render('index', [
'searchModel' => $searchModel,
'model' => $model,
'dataProvider' => $dataProvider,
]);
}
for actionIndex, for showing my index page.
with code above it will show data which has user_id==$userId AND parent_id==$userId.
But that's not what I need, what I need is the page show data which has user_id==$userId OR parent_id==$userId.
How do I can do that?
How do I can set OR condition in queryParams?
Thanks
In your search() method just simply add andWhere() condition:
$query->andWhere(['or',
['user_id' => $this->user_id],
['parent_id' => $this->user_id],
]);
Or in controller:
$dataProvider->query->andWhere(['or',
['user_id' => $userId],
['parent_id' => $userId],
]);
You can use where with or operator
$dataProvider->query->andWhere(['or',
['user_id'=>$user_id],
['parent_id'=>$user_id]
]);
The question is confusing, but I'll explain.
I have this search query from AsistenciaSearch.php
public function search($params)
{
$query = Asistencia::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->joinWith('rutAlumno0');
$query->joinWith('idPlanificacion0');
// grid filtering conditions
$query->andFilterWhere([
'idAsistencia' => $this->idAsistencia,
//'idPlanificacion' => $this->idPlanificacion,
]);
$query->andFilterWhere(['like', 'asistencia', $this->asistencia])
->andFilterWhere(['like', 'rutAlumno', $this->rutAlumno])
//->andFilterWhere(['like', 'idPlanificacion', $this->idPlanificacion])
->andFilterWhere(['like', 'alumno.nombreAlumno', $this->nombreAlumno])
->andFilterWhere(['like', 'alumno.apellidoAlumno', $this->apellidoAlumno])
->andFilterWhere(['like', 'alumno.cursoAlumno', $this->cursoAlumno])
->andFilterWhere(['like', 'alumno.establecimientoAlumno', Yii::$app->user->identity->escuelaProfesor]);
return $dataProvider;
}
And this a controller function using the search query in PlanificacionController.php:
public function actionVerasistencia($id)
{
$searchModel = new AsistenciaSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('verasistencia', [
'model' => $this->findModel($id), //findModel from Planificacion
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Both Asistencia and Planificacion are related by using a primary key in Planificacion named idPlanificacion and a foreign key from that model in Asistencia using the same name.
The question is, I need to make merge with another filter, where the $id from findModel($id) is like the $idPlanificacion from the search query, like this:
public function actionVerasistencia($id)
{
$searchModel = new AsistenciaSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('verasistencia', [
'model' => $this->findModel($id),
'searchModel' => $searchModel,
'dataProvider' => $dataProvider->andFilterWhere('like',$id,$this->idPlanificacion),
]);
}
But I got this error:
Getting unknown property: frontend\controllers\PlanificacionController::idPlanificacion
Any solution, please?
$this inside the controller is related to the controller itself
but your are referring to idPlanificacion alias you are referring to a model attribute
could be you want retrive the value by the model eg:
$model = $this->findModel($id)
so could be
public function actionVerasistencia($id)
{
$searchModel = new AsistenciaSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$model = $this->findModel($id);
return $this->render('verasistencia', [
'model' =>$model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider->andFilterWhere('like',$id,$model->idPlanificacion),
]);
}
Can you help me out with implementing filters to GridView in Yii2? Right now, my rendered table does not respond to my actions (search GET params are not added, nothing changes if I enter a query to a filter input). Here's my code:
Controller:
$searchModel = new UserSearch();
$dataprovider = $searchModel->search(\Yii::$app->request->get());
return $this->render('index', [
'dataProvider' => $dataprovider,
'searchModel' => $searchModel
]);
Model (UserSearch.php):
public $fullname;
public function rules()
{
return [
[['fullname'], 'safe'],
];
}
public function search($params) {
$query = StUsers::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if(!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere(['LIKE', 'fullname', $this->fullname]);
return $dataProvider;
}
View:
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'id',
'fullname'
],
]);
I have the same problem when filtering in DataView. Perhaps the problem is in the client side.
Check again if your jquery called twice on your page ( browser/source code ).
May be your problem related with this also :
jQuery(...).yiiGridView is not a function
There should be no need for a solution since 4 years have passed. But, problem is in next statement:
if(!($this->load($params) && $this->validate())) {
return $dataProvider;
}
Change it to:
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
possibly issue is jquery.min.js
You should referencing not more than once
Try to see if you have repeated.
I don't know how to set the filter default of GridView. It's mean when page loaded, it's will load the filter with specific condition that I've set.
Any idea for this?
Thanks
A simple way to do this is by using the search model .
I am using Default Gii generated code to explain the ways
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Say you want a dynamic filter when the page is loaded
use the link as
../index.php?r=user/index&UserSearch[id]=7
This will add a filter where id = 7 ie in my case since id is the primary key only one user will be listed
Say if you want always apply a filter without showing anything in the url
public function actionIndex()
{
$searchModel = new UserSearch();
$searchModel->name = 'mid';
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
This will create a filter where user's name has string 'mid'
if you want more advanced filters
you can edit the search() function in the UserSearch Class there the query used to populate the data and ActiveDataProvider will be available to you .
say you do't want to list users who are inactive .
public function search($params)
{
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
$query->andFilterWhere(['active_status' => 1]);
....
this method will provide you with limitless ways to filter your results ..
Hope this helps ..
I had the same problem and it worked for me
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andFilterWhere(['status'=>1]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
This helps performing the filter for action is needed and for all, in my case I needed it alone in an environment
public function actionIndex()
{
$searchModel = new UserSearch();
// Filtro por Defecto y Reflejado en Formulario de Filtrado en Grid
$params = Yii::$app->request->queryParams;
if (!isset($params['UserSearch'])) {
$params['UserSearch']['status']=1;
}
$dataProvider = $searchModel->search($params);
// -----------------------------------------------------------
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
NOTA: Usar (!isset($params['UserSearch'])) para que solo se aplique como búsqueda por defecto ('Si no se ha definido ninguna condición de filtrado')
NOTE: Use (**! Isset ($ params ['UserSearch']) **) so that it only applies as a default search ('If no filter condition has been defined')
Yii2 ActiveDataProvider it self need a query builder, means you can filter your results when passing it the query object eg:
$query = Post::find()->where['status' => 'published'];
// Todo and more conditions with $query object
$provider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
]);
A bit late, but only to keep a record on SO.
One way of setting the allowed filters in a Yii2 GridView widget is to use its filterModel object's rules function to return the wanted filtering fields set with the save attributes. So you can remove from this list all the unwanted filters not needed to be displayed in the GridView.
You can then customize the ActiveDataProvider query under the search function of the filterModel to properly build the requested filtered data.