Yii2 Add custom query in dataProvider without rewrite query multiple times - php

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,
]);

Related

Yii2: how to join models?

I have a model called MovementsSearch.php with the typical search($params) function that generates the data provider for actionIndex() function of the MovementsController.php.
search($params) in MovementsSearch.php:
public function search($params)
{
$query = Movements::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id_movement' => $this->id_movement,
]);
return $dataProvider;
}
actionIndex() in MovementsController.php:
public function actionIndex()
{
$searchModel = new MovementsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
I need to join all these tables but I can't do it. Maybe it is possible to join the data provider of each model. It will result in a big table with a lot of columns but then I know how to select the columns that I need.
administrators_offices and products_offices are many-to-many relations.

How to merge filters from one search model to one findModel from a controller in Yii 2?

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

Yii2 Many to Many with Self - filter through grid view (no attribute?)

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...
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]);

Yii2 Gridview filtering not working

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.

Default Filter in GridView with Yii2

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.

Categories