Cgridview Relations And Scopes

Hello all.

Well, I’m with the following problem about scope and relation on CGridView.

First I did the relationship with the Company table … Where Company and Unit are the same table, it worked fine.

Model Company.php

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(

	'parentID' => array(self::BELONGS_TO, 'Company', 'parent_id'),




$this->widget('zii.widgets.grid.CGridView', array(






		'value' =>'(isset($data->parentID->name)) ? $data->parentID->name : ""',

		'filter' =>CHtml::listData(Company::model()->findAll(), 'id', 'name'),



I would like to show the same table Company in two different ways depending on the menu being clicked. For example, if you click on the Unit table is filtered as the scope 'parent_id is not null "and the Company the scopo is ’ parent_id is null '. It worked fine as well.

public function defaultScope()


	$company = array('condition'=>"parent_id is null",);

	$unit = array('condition'=>"parent_id is not null",);

	if($_GET['LEVEL'] == 1) {

		return $unit;

	} else {

		return $company;



Both work well if used separately, but when I try to use the relationship and scope I have problems with the relationship.

Any help?

Sorry for my english!

Any?? =(