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.