I'm trying display and filter data which I got via SQL SUM operator.
I have 2 table employee and department. Table employee contains department_id filed and salary filed. I need display all department and total SUM salary for every department.
I followed this guide, but GridView does not display any data.
Here is action:
public function actionIndex()
{
$searchModel = new DepartmentFrontendSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index',
[
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]
);
}
Model:
class DepartmentFrontendSearch extends DepartmentFrontend
public $employee_count;
public $salary;
public function rules() {
return [
[['employee_count','salary','name'], 'safe']
];
}
public function search($params) {
$query = DepartmentFrontend::find();
$subQuery = Employee::find()
->select('department_id, SUM(salary) as salary_amount')
->groupBy('department_id');
$query->leftJoin(['salarySum' => $subQuery], 'salarySum.department_id = id');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
'name',
'salary'
]
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['like', 'name', $this->name]);
$query->andWhere(['salarySum.salary_amount' => $this->salary]);
return $dataProvider;
}
GRID:
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'salary_amount',
'employee_count',
'name',
],
]);
So, can anybody tell me what I did wrong?
If You need a filter for the alias salary_amount you should add a specific var for this (do the fact salary_amount is an alias for sum(salary) the var $salary is not useful)
public $employee_count;
public $salary;
public $salary_amount;
otherwise use salary as alias too
the in your filter you are using
$query->andWhere(['salarySum.salary_amount' => $this->salary]);
But salary_amount is the result of an aggregation so you should use having(SUM(salary) = $this->salary_amount)
$query->having('SUM(salary) = ' . $this->salary_amount);
Related
To show my GridView I use this ActiveDataProvider:
public function search($params)
{
$query = PublicationsPublication::find()
->select(['eid', 'title', 'pubdate', 'citedby', "STRING_AGG(DISTINCT(CONCAT(author.authid, ' - ', authname)), ', ') AS authors"])
->joinWith('publicationsAuthor')
->groupBy(['eid','title','pubdate','citedby']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
...
}
I can't figure out how to use the column generated by the STRING_AGG() function in the Gridview.
Just in case is needed, the publicationsAuthor relation is coded this way:
public function getPublicationsAuthor() {
return $this->hasMany(PublicationsAuthor::className(), ['authid' => 'authid'])
->viaTable('publications.pub_author', ['pubid' => 'id']);
}
I need to use the STRING_AGG() function because I want to show many authors in one cell of the Gridview.
I tried to use the "authors" column in this way:
$gridColumns = [
[
'class' => 'kartik\grid\SerialColumn',
'width' => '20px',
],
'eid',
'title',
'pubdate',
'citedby',
'authors',
];
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last'
],
...
]);
But unfortunately it didn't work. In the Grid all the values are set to "not set". The query works great because I tested it in PgAdmin.
yii\data\ActiveDataProvider works with models so only fields defined by model are available by default. The easiest way to add field generated by some expression is to add public property with same name to your model like this:
class PublicationsPublication extends ActiveRecord
{
public $authors;
// ... other code in PublicationsPublication model ...
public function attributeLabels()
{
// You can also add label for new field
return [
'authors' => 'Authors',
// ... other labels ...
];
}
}
The public property $authors will be loaded with data from field authors in the result of your SQL query. Then you can use it in grid as any other field.
The other option is to use yii\data\SqlDataProvider instead of yii\data\ActiveDataProvider.
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.
Can anyone give me sample of how to retrieve data from model without having to use Widget ? Because need to get data per table column and put them inside my own view (not using widget)
Controller :
public function actionIndex() {
$searchModel = new B2CProductsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
View :
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'sku',
'name',
'short_description',
'long_description',
'thumb_img:ntext',
'large_img:ntext',
'url_content:ntext',
'contact_info',
'status',
'currency',
'price',
'dimension',
'weight',
// 'created',
// 'modified',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
The widget above iterate through the element inside the predefined gridview,
but what I want to do instead is something like :
foreach($data as $ab) {
echo $ab->id;
}
You can do it this way:
/* assuming $data is an ActiveDataProvider object */
$models = $data->getModels();
foreach($models as $model) { ... }
see more here
If you are using an ActiveDataProvider:
Controller:
public function actionMyAction()
{
$dataProvider = new ActiveDataProvider([
'query' => MyModel::find(),
]);
return $this->render('my-results-page', [
'dataProvider' => $dataProvider,
]);
}
View:
<?php
foreach ($dataProvider->models as $model){
echo $model->myProperty;
}
?>
If you are using a query, such as this:
$query = (new \yii\db\Query())
->select('this, that')
->from('over_there');
$command = $query->createCommand();
$rows = $command->queryAll();
Then you can iterate over the result like this(assuming you passed it to the view in a variable called $dataProvider):
foreach($dataProvider as $data)
{
$data['myProperty'];
}
Following on from this:
Yii2 how does search() in SearchModel work?
I would like to be able to filter a GridView column of relational data. This is what I mean:
I have two tables, TableA and TableB. Both have corresponding models generated using Gii. TableA has a foreign key to a value in TableB, like this:
TableA
attrA1, attrA2, attrA3, TableB.attrB1
TableB
attrB1, attrB2, attrB3
attrA1 and attrB1 are the primary keys of their corresponding tables.
Now, I have a Yii2 GridView of attrA2, attrA3 and attrB2. I have a working filter on attrA2 and attrA3 so that I can search on column values. I also have a working sort for these two columns too - by just clicking on the column header. I would like to be able to add this filtering and sorting on attrB2 too.
My TableASearch model looks like this:
public function search($params){
$query = TableA::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$this->addCondition($query, 'attrA2');
$this->addCondition($query, 'attrA2', true);
$this->addCondition($query, 'attrA3');
$this->addCondition($query, 'attrA3', true);
return $dataProvider;
}
In my TableA model, I set the related value like this
public $relationalValue;
public function afterFind(){
$b = TableB::find(['attrB1' => $this->attrB1]);
$this->relationalValue = $b->relationalValue;
}
Although it is probably not the best way of doing this. I think I have to use $relationalValue somewhere in my search function but I'm not sure how. Similarly, I would like to be able to sort by this column too - just like I can for attrA2 and AttrA3 by clicking on the header link`. Any help would be appreciated. Thanks.
This is based on the description in the guide. The base code for the SearchModel comes from the Gii code generator. This is also assuming that $this->TableB has been setup using hasOne() or hasMany() relation. See this doc.
1. Setup search model
In TableASearch model add:
public function attributes()
{
// add related fields to searchable attributes
return array_merge(parent::attributes(), ['TableB.attrB1']);
}
public function rules()
{
return [
/* your other rules */
[['TableB.attrB1'], 'safe']
];
}
Then in TableASearch->search() add (before $this->load()):
$dataProvider->sort->attributes['TableB.attrB1'] = [
'asc' => ['TableB.attrB1' => SORT_ASC],
'desc' => ['TableB.attrB1' => SORT_DESC],
];
$query->joinWith(['TableB']);
Then the actual search of your data (below $this->load()):
$query->andFilterWhere([
'like',
'TableB.attrB1',
$this->getAttribute('TableB.attrB1')
]);
2. Configure GridView
Add to your view:
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
/* Other columns */
'TableB1.attrB1',
/* Other columns */
]
]);
Filtering a gridview by a column is damn easy in Yii 2.0. Please add the filter attribute to a gridview column having lookup values, as under:
[
"class" => yii\grid\DataColumn::className(),
"attribute" => "status_id",
'filter' => ArrayHelper::map(Status::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
"value" => function($model){
if ($rel = $model->getStatus()->one()) {
return yii\helpers\Html::a($rel->name,["crud/status/view", 'id' => $rel->id,],["data-pjax"=>0]);
} else {
return '';
}
},
"format" => "raw",
],
I'm stuck with this problem too, and my solution is rather different. I have two simple models:
Book:
class Book extends ActiveRecord
{
....
public static function tableName()
{
return 'books';
}
public function getAuthor()
{
return $this->hasOne(Author::className(), ['id' => 'author_id']);
}
And Author:
class Author extends ActiveRecord
{
public static function tableName()
{
return 'authors';
}
public function getBooks()
{
return $this->hasMany(Book::className(), ['author_id' => 'id']);
}
But my search logic is in different model. And i didn't find how can i implement search without creating additional field author_first_name. So this is my solution:
class BookSearch extends Model
{
public $id;
public $title;
public $author_first_name;
public function rules()
{
return [
[['id', 'author_id'], 'integer'],
[['title', 'author_first_name'], 'safe'],
];
}
public function search($params)
{
$query = Book::find()->joinWith(['author' => function($query) { $query->from(['author' => 'authors']);}]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => array('pageSize' => 50),
'sort'=>[
'attributes'=>[
'author_first_name'=>[
'asc' => ['author.first_name' => SORT_ASC],
'desc' => ['author.first_name' => SORT_DESC],
]
]
]
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
....
$query->andWhere(['like', 'author.first_name', $this->author_first_name]);
return $dataProvider;
}
}
This is for creating table alias: function($query) { $query->from(['author' => 'authors']);}
And GridView code is:
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'id',
'filter' => false,
],
[
'attribute' => 'title',
],
[
'attribute' => 'author_first_name',
'value' => function ($model) {
if ($model->author) {
$model->author->getFullName();
} else {
return '';
}
},
'filter' => true,
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I will appreciate any critiques and advice.