Yii Framework Forum: Relation X2, Condition In Cactivedataprovider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relation X2, Condition In Cactivedataprovider Rate Topic: -----

#1 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 13 February 2013 - 12:47 PM

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
0

#2 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 16 February 2013 - 03:24 AM

No one...?

Thought it was an easy-to-fix problem for Yii eksperts...? :/
0

#3 User is offline   clapas 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 21-February 11

Posted 16 February 2013 - 06:38 AM

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

#4 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 386
  • Joined: 08-June 10
  • Location:France (Ile-de-France/Val d'Oise)

Posted 16 February 2013 - 04:20 PM

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.
0

#5 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 18 February 2013 - 06:58 AM

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

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users