Using the same relation multiple times with different scopes in one CActiveDataProvider

Hello everyone!

I’ve started using Yii for a large project at work. I’m very happy with the setup and performance so far. :slight_smile:

I’m currently a bit stuck however. I want to display a list of websites that are attached to a customer and for each website I want to render the sum of the registered subsites (belonging to these websites). So far, no problem.

However, each of those subsites may have some elements attached to them. These elements each have a status that may be pending, active or inactive.

So, in the model for the website I have defined a relation to the subsites, and a relation to the elements through the subsites. Additionaly, I defined three scopes for the SubsiteElement Model to identify each status.




/* ============================================================

 * Website Model

 * ============================================================ */

 

public function relations()

{

	return array(

		'subsites'        => array(self::HAS_MANY, 'Subsite',        'websiteid'),

		'subsiteCount'    => array(self::STAT,     'Subsite',        'websiteid'),

		'subsiteElements' => array(self::HAS_MANY, 'SubsiteElement', 'subsiteid', 'through' => 'subsites'),

	);

}





/* ============================================================

 * SubsiteElement Model

 * ============================================================ */

 

public function scopes()

{

	return array(

			'isPending'  => '`status` = \''.self::STATUS_PENDING .'\'',

			'isActive'   => '`status` = \''.self::STATUS_ACTIVE  .'\'',

			'isInactive' => '`status` = \''.self::STATUS_INACTIVE.'\'',

		),

	);

}



Now, inside the controller, I want to create an CActiveDataProvider that loads all websites, a count for the number of subsites and a column for each status of the subsite elements below each website…




$dataProvider = new CActiveDataProvider('Website', array(

	'criteria' => array(

		'condition' => '`t`.`customerid` = :cid',

		'params'    => array(':cid' => $this->customer->customerid),

		'order'     => 'title ASC, websiteid ASC',

		'with'      => array(

			'subsiteCount'    => array('scopes' => array('isAvailable')),

			'subsiteElements' => array('scopes' => array('isPending')),

			'subsiteElements' => array('scopes' => array('isActive')),

			'subsiteElements' => array('scopes' => array('isInactive')),

		),

	),

	'pagination' => array(

		'pageSize' => 20,

	),

));



Of course, this will not work. But i’m currently unable to find anything on the Yii documentation about how to do it. Is there a functionality to refer to a relation but use an alias towards the CActiveDataProvider? So that you can use the same relation multiple times but with different scopes.

Thanks in advance! :slight_smile:

Try to define a relation for each SubsiteElement status in Website model:





public function relations()

{

	return array(

    	'pendingElements' => array(self::HAS_MANY, 'SubsiteElements', 'subsiteid', 'through' => 'subsites', 'on' => 'pendingElements.status = " ' . SubsiteElement::STATUS_PENDING . '"'),

    	//...etc

	);

}