Hello!
I have three models, - name, city, population - related in series
name.id_city -> city.city_name
and
city.id_population -> population->pop_amount
So i need table view with id(name), name, city and population
The trouble is - i need to use cgridview with several indirectly related models.
Now i can show data from name and city, or from city and population, but not all together.
Here is my code:
Model ‘name’
// models/name.php
class name extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'name';
}
public function rules()
{
return array(
array('id, name_person, nameCity', 'safe', 'on'=>'search'),
);
}
public function attributeLabels()
{
return array(
'id' => 'Id',
'name_person' => 'Name',
'nameCity' => 'City',
);
}
public function relations()
{
return array(
'nameCity'=>array(self::BELONGS_TO, 'city', 'id_city'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('t.id',$this->id);
$criteria->compare('name_person',$this->name_person,true);
$criteria->with=array('nameCity');
$criteria->compare('nameCity.name_city',$this->nameCity,true);
return new CActiveDataProvider('name', array(
'criteria'=>$criteria,
'sort'=>array('attributes'=>array(
'nameCity'=>array(
'asc' => $expr='nameCity.name_city',
'desc' => $expr.' DESC',
),
'id'=>array(
'asc' => $expr='t.id',
'desc' => $expr.' DESC',
),
'name_person',
)),
'pagination'=>array(
'pageSize'=>40,
),
));
}
}
Model ‘city’
<?php
// models/city.php
class city extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'city';
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'name_city' => 'City',
'populationCity' => 'Population'
);
}
public function relations()
{
return array(
'populationCity'=>array(self::BELONGS_TO, 'population', 'id_population'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('t.id',$this->id);
$criteria->compare('name_city',$this->name_city,true);
$criteria->with=array('populationCity');
$criteria->compare('populationCity.pop_amount',$this->populationCity,true);
return new CActiveDataProvider('city', array(
'criteria'=>$criteria,
'sort'=>array('attributes'=>array(
'populationCity'=>array(
'asc' => $expr='populationCity.pop_amount',
'desc' => $expr.' DESC',
),
'id'=>array( // сортировка по id
'asc' => $expr='t.id',
'desc' => $expr.' DESC',
),
'name_city',
)),
'pagination'=>array(
'pageSize'=>40,
),
));
}
}
Model ‘population’
// models/population.php
class population extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'population';
}
}
Function for it i’ve put in ‘SiteController’
public function actionView()
{
$model_name=new name('search');
if(isset($_GET['name']))
$model_name->attributes=$_GET['name'];
$model_city=new city('search');
if(isset($_GET['city']))
$model_city->attributes=$_GET['city'];
$this->render('view',array(
'model_name'=>$model_name,
'model_city'=>$model_city,
));
}
And ‘view’
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'name-grid',
'dataProvider'=>$model_city->search(),
'filter'=>$model_city,
'columns'=>array(
'id',
/*'name_person',
array(
'name'=>'nameCity',
'value'=>'$data->nameCity->name_city',
'sortable'=>true,
),*/
'name_city',
array(
'name'=>'populationCity',
'value'=>'$data->populationCity->pop_amount',
'sortable'=>true,
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
So, is there an option to use several data providers?
Or, maybe, there are better ways for this - just show me where to go, please)