Yii Framework Forum: Question When Using "with" And "scopes" - Yii Framework Forum

Jump to content

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

Question When Using "with" And "scopes" Rate Topic: -----

#1 User is offline   georaldc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 08-July 11

Posted 07 December 2012 - 12:16 PM

I've been trying to use scopes inside the with option and haven't been able to figure out how. Here's an example setup similar to mine:

ParentModel has the following scopes
"getValidatedEntries" => array(
    "condition" = "valid = 1",
)


ChildModel belongs to ParentModel

I'm trying to use the scope like this:

$validChildModels = $childModel(array(
	'with' => array(
		'parentModel' => array(
			'scopes' => array(
				'getValidatedEntries',
			),
		),
	),
));


Doing it this way, I end up losing parentModel for some reason ($validChildModel->parentModel becomes null)

But if I run the code without using the scope like this

$validChildModels = $childModel(array(
	'with' => array(
		'parentModel' => array(
			'condition' = 'valid = 1',
		),
	),
));


it works fine. Any idea what I'm doing wrong?
0

#2 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,082
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 07 December 2012 - 03:42 PM

I think you have do something as following
childModel::model()->with('parentModel:getValidatedEntries')->findAll()



Note: In case it does not work just double check the syntax
0

#3 User is offline   yugene 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 513
  • Joined: 08-August 09

Posted 08 December 2012 - 04:25 AM

Hm, your request looks fine to me at the first look. I'd first check the resulting sql query you get.
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