how to convert query with multiple group by columns to CActiveDataProvider in Yii 1.1.14? - yii

I need to conver this sql statement that has a multi column group by clause into CActiveDataProvider in Yii version 1.1.4?
select * from my_table_name group by bookname,categorytitle,bookkey order by bookname,categorytitle,bookkey asc;
I need a CActiveDataProvider to feed a CGridView search function. Apparently, I only have a simple return in my model of the search function that runs my CGridView
return new CActiveDataProvider(get_class($this),
array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 'id ASC'
),
'pagination' => array('pageSize' => ActiveRecord::PAGE_SIZE),
));
so how to convert the sql statement that I gave into CActiveDataProvider format ?

You can do using two way -
Make sql view and than create yii model and use this in your
controller or page view.
Create custom search function in your existing model and use group by
e.g. -
<?php
public function new_search(){
$criteria = new CDbCriteria();
...
..
$criteria->group = "bookname, categorytitle, bookkey";
$criteria->order = "bookname, categorytitle, bookkey";
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>100)
));
}
?>

Related

How to get names in alphabetic order in Yii

In my Yii web application, having student table for saving student details. I want to student name in alphabetical order when retrieve data from student table.
public function defaultScope() {
return array("order" => "student_firstname");
}
I tried this function, but not working properly.
Please help me.
Thanks in advance.
you can use the defaultOrder property of CSort. For example...
$dataProvider=new CActiveDataProvider('Example', array(
'sort'=>array(
'defaultOrder'=>array(
'student_firstname'=>false
)
)
));
In model's search method for data provider try to set default order, like this:
$dataProvider=new CActiveDataProvider($this, array(
'sort'=>array(
'defaultOrder'=> 'student_firstname ASC'
)
));
In model's search method for data provider try to set default order, like this:
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>Yii::app()->params['defaultPageSize'],
),
'sort'=>array(
'defaultOrder'=>array(
'student_firstname'=>CSort::SORT_ASC
),
),
));

yii CGridView dataprovider and filter

I know we can show a gridview with a model and it's search method and filter the results, but can we make a gridview with another dataprovider and another model like this and filter its results? Does filter needs to be a part of dataprovider?
$attr = Yii::app()->request->getParam($name);
$model = new User('search');
$model->unsetAttributes();
$model->setAttributes($attr);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $myDataProvider,
'filter' => $model,
'columns' => array(
array(
'name' => 'username',
'type' => 'raw',
'value' => 'CHtml::encode($data->username)'
),
array(
'name' => 'email',
'type' => 'raw',
),
),
));
The above code doesn't work and I need to add a filter on a previously made data provider.
Btw $attr has a valid data, but grid is not filtered.
$model doesn't affect $myDataProvider since the data provider is not obtained using this model.
$model->search() returns a CActiveDataProvider which contains a CDbCriteria instance. Different CDbCriteria can be combined using mergeWith(). So if you would like the data to be filtered using the values from the $model
...
$model->setAttributes($attr);
$newDataProvider=$model->search();
$myDataProvider->criteria->mergeWith($newDataProvider->criteria);
$this->widget('zii.widgets.grid.CGridView', array(
...
Filter does not need to be a part of dataprovider, but data provider needs to take the model into account, if you want to use it for filtering.
The way this is done by default is to create the data provider using search method on your model, which sets conditions of your data provider based on model values, like so:
'dataProvider' => $model->search()
There is nothing preventing you from creating different data provider, for example:
'dataProvider' => $model->createAnotherDataProvider()
And in your User model:
public function createAnotherDataProvider() {
{
// create your second data provider here
// with filtering based on model's attributes, e.g.:
$criteria = new CDbCriteria;
$criteria->compare('someAttribute', $this->someAttribute);
return new CActiveDataProvider('User', array(
'criteria' => $criteria,
));
}

How to sort the fields from relation table in Yii?

I am trying to implement sorting with Yii list view. I have joined 2 tables named provider_favourite and service_request in list view. And the fields and contents from both tables are listing in the list view. But sorting is working only in provider_favourite table, not from service_request table.How can I sort the fields from service_request table? Iam using csort for sorting. I also tried CGrid view. But the same problem is happening in grid view also ..
Iam using the following code to join
$criteria = new CDbCriteria;
$criteria->select = 'favourite_notes,favourite, favourite_added_date,max_budget,preferred_location,service_name';
$criteria->join = 'LEFT JOIN service_request AS s ON service_request_id = favourite';
$criteria->condition = 'favourite_type = 1';
$sort=new CSort('ProviderFavourite');
// $sort->defaultOrder='s.max_budget ';
$sort->applyOrder($criteria);
$sort->attributes = array(
'max_budget' => 'service_request.max_budget',
'service_name' => 'service_request.service_name',
'favourite_added_date'
);
$type = 2;
$data = new CActiveDataProvider('ProviderFavourite', array('criteria' => $criteria, 'pagination' => array('pageSize' => 4),'sort'=>$sort
));
$this->renderPartial('favourites', array(
'ModelInstance' => ProviderFavourite::model()->findAll($criteria),
'dataProvider' => $data, 'type' => $type, 'sort'=>$sort,
));
and also Iam providing sortable attributes in list view
$this->widget('zii.widgets.CListView', array('dataProvider'=>$dataProvider,'itemView'=>'index_1',
'id'=>'request',
'template' => ' {items}{pager}',
'sortableAttributes'=>array('favourite_notes','max_budget','service_name')
));
If more details needed, I will provide. Thanks in advance
You have to specify attributes property of your $sort instance. By default only fields of $modelClass (ProviderFavourite in your case) are sortable.
I think it could look like this (not tested):
$sort->attributes = array(
'service_name' => array(
'asc' => 's.service_name ASC',
'desc' => 's.service_name DESC'
),
// ...another sortable virtual attributes from service_request table
"*"
);
You should not create a CSort object in $sort. The CActiveDataProvider will already provide you with the right sort object. The way you do it, you apply the sort criteria to $criteria before you configure the sort attributes. That can not work.
You should try a simple setup like this instead:
$data = new CActiveDataProvider('ProviderFavourite', array(
'criteria' => $criteria,
'pagination' => array('pageSize' => 4),
'sort'=> array(
'attributes' => array(
'service_name' => array(
'asc' => 's.service_name ASC',
'desc' => 's.service_name DESC'
),
// ...
),
));
If you need to access the related CSort object (which you usually don't if you use a CGridView or CListView, because they deal with that for you), then you get it via $data->sort.

Use column alias in dataProvider

In my model I have a relation() like this.
'notRelUser' => array(
self::HAS_MANY,
'LocationUser',
'location_id',
'condition' => 'notRelUser.status is null',
'on' => 'notRelUser.user_id = ' . Yii::app()->user->getId(),
'with' => array('parent_location'),
'select' => array('*', 'name AS canApply'),
),
and this
public $canApply;
In my controller I have this
$regions = Location::model()->with('notRelUser')->findAll();
$arrayCanApply = new CArrayDataProvider($regions);
I then am trying to print out the value of canApply in a widget for datagrid
<?php $this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider'=>$arrayCanApply,
'columns'=>array(
array('name'=>'name', 'header'=>'Name'),
array('name' => 'canApply', 'value'=>'$data->canApply', 'header'=>'CanApply'),
),
));
But the column with canApply is empty.
I have also tried without the $data-> part.
How can I print this alias?
(I know the value of the alias i trivial, but I will change that later)
UPDATE:
I can get the value by using select in CDbCriteria - but I want to do it in relation.
This does work.
$criteria = new CDbCriteria;
$criteria->select = '("foobar") AS canApply';
$regions = Location::model()->with('notRelUser')->findAll($criteria);
but it seems odd that I have to create a $criteria on everytime
you need to declare can canApply as an attribute of the model
in your model decalare canApply as an attribute
public $canApply;
I also had the same problem. Try 'value'=>'$data->notRelUser->canApply'
instead of 'value'=>'$data->canApply'. Also, you need to declare attribute
public $canApply;
in your LocationUser model, not in Location model.

Cgridview with data from 2 unrelated models

I am slowly advancing yii
Not how to apply this:
I have a CGridView which shows me data from 3 related tables
I do it this way (modify the default generated by the crud:
model tabla1
public function search()
{
...
...
...
$criteria=new CDbCriteria;
$criteria->with = array('tabla2','tabla3');
$criteria->together = true;
$criteria->addInCondition('t.idtb1',1,3,5,6,7);
$criteria->compare('idtb2',$this->idtb2);
$criteria->compare('date',$this->date,true);
$criteria->compare('tabla2.codigo',$this->codigo,true);
$criteria->compare('tabla3.description',$this->descrip,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
The view
...
...
...
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'redeemed-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'codigo',
'value'=>'$data->tabla2->codigo',
),
array(
'name'=>'description',
'value'=>'$data->tabla3->descrip',
),
This shows me everything works perfect.
The problem is that I want to use two more tables that are not related, they are tabla4 and tabla5 and aggregate data from both tables to Cgridview.
  as can be done for linking data to table1 to be unrelated to tabla4 and tabla5?
Greetings and thanks