Cgridview with data from 2 unrelated models - yii

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

Related

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

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)
));
}
?>

cgridveiw to display fetched data via customized query

I am a newbei in yii, when a person clicks on a category display him all products under that particular category in a gridview
view
productcategory
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'admin-grid',
'dataProvider'=>$model->SelectedCategoryProducts,
'filter'=>$model,
'columns'=>array(
'Name',
'Model',
'Brand',
'Price',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
controller
product
public function actionProductcategory($id)
{
$model= Product::model()->SelectedCategoryProducts($id);
var_dump($model);
$this->render('productcategory',array(
'model'=>$model,'id'=>$id,
));
}
model
product
public function SelectedCategoryProducts($id)
{
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
var_dump($dataProvider);
return $dataProvider;
}
CException
Property "CActiveDataProvider.sellerSelectedCategoryProducts" is not defined.
PLEASE HELP! I am losing my mind on this ... not able to display in gridview.
Pass $id to your view file.
$this->render('productcategory',array('model'=>$model,'id'=>$id));
Then pass id to model function in ccgridview function.
'dataProvider'=>$model->SelectedCategoryProducts($id),
Hope this might help
Controller file
public function actionProductcategory($id)
{
$model=new Product;
$this->render('productcategory',array('model'=>$model,
'id'=>$id));
}
In View file
'dataProvider'=>$model->SelectedCategoryProducts($id),
UPDATE 1
'columns'=>array(
'name',
'model',
'brand',
'price',
change them to lowercase which are your original column names
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
this can retrieve needed data.... first check that data is perfect ... is it right after that take second step....

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

Yii data provider with clist view

i want to search multiple models in Yii application. The search result are displayed in CList view. Need to use dataprovider in list view. So how can i use multiple dataprovider in Clist view?
You cannot make use of multiple dataproviders rather you combine the result in a single dataprovider
Something like this to get you started:
public function actionSearch($q) {
// Sanitize input
$q = strtolower(strip_tags($q));
$q = preg_replace('/[^a-z 0-9 _ \- \']/', '', $q);
$model1 = Model1::model()->findAll('title LIKE "%'.$q.'%"');
$model2 = Model2::model()->findAll('title LIKE "%'.$q.'%"');
$rawData = array_merge($model1, $model2);
$dataProvider = new CArrayDataProvider($rawData, array(
'sort'=>array(
'attributes'=>array(
'datePublished DESC', 'title',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
$this->render('search', array(
'dataProvider' => $dataProvider,
'query' => $q,
));
}

custom search dropdown in yii

I have a database containing 3 tables, countries, cities and datacenters. country is the parent of city and city is the parent of datacenter. In the cities I have added a search dropdown of countries and its working fine. I have added cites search dropdown in datacenters and its also working fine but now i want countries search dropdown in datacenters, how to do it.
Code in datacenters for countries search dropdown in admin.php.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'data-centers-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'city.cityname'=>array(
'name'=>'cityid',
'value'=>'$data->city->cityname',
'filter'=>CHtml::listData(Cities::model()->findAll(array('order'=>'cityname')), 'id', 'cityname')
),
'city.country.countryname'=>array(
'name'=>'city.country.id',
'value'=>'$data->city->countryid',
'filter'=>CHtml::listData(Countries::model()->findAll(array('order'=>'countryname')), 'id', 'countryname')
),
'datacentername',
'datacentercode',
'lastupdatedon',
'createdon',
array(
'class'=>'CButtonColumn',
'template'=> '{view}{update}',
),
),
)); ?>
model code:
class DataCenters extends CActiveRecord
{
[...]
/**
* #return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'lastupdatedby0' => array(self::BELONGS_TO, 'Users', 'lastupdatedby'),
'city' => array(self::BELONGS_TO, 'Cities', 'cityid'),
'servers' => array(self::HAS_MANY, 'Servers', 'datacenterid'),
);
}
[...]
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('cityid',$this->cityid);
$criteria->compare('datacentername',$this->datacentername,true);
$criteria->compare('datacentercode',$this->datacentercode,true);
$criteria->compare('lastupdatedon',$this->lastupdatedon,true);
$criteria->compare('lastupdatedby',$this->lastupdatedby);
$criteria->compare('createdon',$this->createdon,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
[...]
}