Relation X2, Condition In Cactivedataprovider

Short story: I want to create a CActiveDataProvider with a condition of a relation, of a relation equals "Wool".

After a search on the forum I’m still not able to solve this problem…

I’m posting the last reviews on the site, and works just fine:




$reviews = new CActiveDataProvider('Reviews', array(

	'criteria' => array(

		'order' => 'reviewDate DESC',

		'limit' => 5

	),

	'pagination' => false

));



But now i want to create a condition from a relation, to a relation…

In a find function it looks like:


$reviews->merchant->merchantssubscriptions->subscription

and works just fine.

More explanation:

In my reviews model i’ve got a relation to merchant, looking like this:


'merchant' => array(self::BELONGS_TO, 'Merchants', 'merchantId'),

And merchants model like this:


'merchantssubscriptions' => array(self::HAS_ONE, 'Merchantssubscriptions', 'merchantId'),

I then want to get "merchantssubscriptions->subscription" as a condition…

I’m not sure how i can do this… Maybe with the “With” function in CActiveDataProvider, but after a lot of different tries i havent found a solution.

Hope somebody can help.

And please let me know if you need more information…

Thanks a lot.

Casper Skou

No one…?

Thought it was an easy-to-fix problem for Yii eksperts…? :confused:

I’m not sure if I understand you well, but named scopes may be what you are looking for.

Something like this should do:




'criteria'=>array(

	[...],

	'with'=>array(

		'merchant'=>array(

			'with'=>array(

				'merchantsubscriptions'=>array(

					'with'=>array(

						'subscription'=>array(

							'condition'=>..., // any type of 'criteria' (scopes, params, ...).

						)

					)

				)

			)

		)

	),



There are shorter ways, but this method splits it out relation by relation.

hii le_top

Thanks a lot - that made the tricks…

For the record, i ended up using the following code (maybe it can help somebody else)…:


$reviews = new CActiveDataProvider('Reviews', array

(

	'criteria'=>array(

        	'with'=>array(

                	'merchant'=>array(

				'condition'=>'merchant.status = :status',

				'params'=>array(':status'=>'Active'),

                    		'with'=>array(

					'merchantssubscriptions'=>array(

						'select'=>false,

						'condition'=>'merchantssubscriptions.subscription != :subscription AND merchantssubscriptions.subscriptionStatus = :subscriptionStatus',

						'params'=>array(':subscription'=>'Cotton', ':subscriptionStatus'=>'Active'),

					)

				)

			)

		)

	),

	'pagination' => false

));