Yii Framework Forum: Parameterized vs named scopes question (using Yii 1.1.8) - Yii Framework Forum

Jump to content

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

Parameterized vs named scopes question (using Yii 1.1.8) scopes Rate Topic: -----

#1 User is offline   acorncom 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 25-February 11
  • Location:CO, USA

  Posted 06 September 2011 - 10:13 AM

Howdy, I'm running into some questions regarding a three-model query I'm trying to run. I'm using a contrived example below to try to make my point clearer.

I'm trying to retrieve a specific User account along with the User's posts and pending comments. But what I'd really like is to have a parameterized scope for my pending comments.

// this query works fine and pulls everything I'm after
$user = User::model()->with('posts.comments:pendingInLastTwoWeeks')->findByPk((int)$userID);

// this is the query I would like to write (using a parameterized scope)
$user = User::model()->with('posts.comments:pendingInXWeeks($x)')->findByPk((int)$userID);

// and I know I can't do this, as it's operating on the User model, not my comments model
$user = User::model()->with('posts.comments')->pendingInXWeeks($x)->findByPk((int)$userID);


According to what I've seen/read, with Yii 1.1.7 (and newer), we have access to querying using different syntax (with more control over scopes, etc.). But I'm having a hard time finding / writing the right syntax:

// I'm trying to replicate with('posts.comments:pendingInTwoWeeks') above and haven't figured out how
// this code doesn't work and I haven't even started putting in parameters for the scope
$user = User::model()->with(array(
	'posts'=>array(
		'relations'=>array(
			'comments'=>array(
				'scopes'=>array(
					'pendingInTwoWeeks'
					)
				)
			)
		)
	))->findByPk($userID);


Any thoughts / suggestions? I've wondered about setting up a parameterized, named function (acting as a scope) with my Post model or resigning myself to just doing multiple queries. But I'd love to actually learn how this works (and how to make it work well for me). I've poured over the original parameterized scope patch back in February (revision here), looked at the AR guides on relational queries repeatedly and checked all over the forums for more info.

But I'm either not finding it or documentation in-depth on how to use these new features doesn't really exist. Any pointers on how to handle it? I'd be happy to help with the documentation, but can't do that until I know what I'm doing :)

Thanks.
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,788
  • Joined: 17-January 09
  • Location:Russia

Posted 07 September 2011 - 02:21 PM

http://www.yiiframew...th-named-scopes
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   acorncom 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 25-February 11
  • Location:CO, USA

Posted 07 September 2011 - 06:26 PM

Hi samdark,
I've read that particular section repeatedly, but either I don't understand what I'm reading or I'm doing something else wrong.

Is there anyway to parameterize pendingInLastTwoWeeks in this line?
$user = User::model()->with('posts.comments:pendingInLastTwoWeeks')->findByPk((int)$userID);


Edit:
Ok, I just got it. Turns out my trouble was that this:
$user = User::model()->with(array(
        'posts'=>array(
                'relations'=>array(    // <---- this needed to be changed
                        'comments'=>array(
                                'scopes'=>array(
                                        'pendingInTwoWeeks'
                                        )
                                )
                        )
                )
        ))->findByPk($userID);


needed to be changed to this:
// to this
$user = User::model()->with(array(
        'posts'=>array(
                'with'=>array(    // <---- this needed to be changed
                        'comments'=>array(
                                'scopes'=>array(
                                        'pendingInXWeeks' => 2
                                        )
                                )
                        )
                )
        ))->findByPk($userID);

// and then in my comments model, we're actually calling a named function

	public function pendingInXWeeks($numberOfWeeks="")
	{
		$this->getDbCriteria()->mergeWith(array(
			'condition' => '', // conditions set here
			'params' => array(':numberOfWeeks'=>(int)$numberOfWeeks),
	    ));
	    return $this;
	}


Samdark, thanks for all your work on the scoping stuff. It makes this so easy and powerful once I grasp the syntax :)
4

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