Yii 'limit' on related model's scope

Posted by pethee on Stack Overflow See other posts from Stack Overflow or by pethee
Published on 2014-08-21T10:35:50Z Indexed on 2014/08/22 10:20 UTC
Read the original article Hit count: 120

Filed under:
|
|
|

I have a model called Guesses that has_many Comments. I'm making eager queries to this to then pass on as JSON as response to an API call.

The relations are obviously set between the two models and they are correct(one2many <=> belongs2)

I added a scope to Comments called 'api' like this:

public function scopes()
    {
        return array(
            'api' => array(
                'select' => 'id, comment, date',
                'limit'=>3,
                'order'=>'date DESC',
                'together'=>true,
            ),
        );
    }

And I'm running the following one-liner query:

$data = Guesses::model()->with('comments:api')->findAll();

The issue here is that when calling the 'api' scope using a with('relation'), the limit property simply doesn't apply. I added the 'together'=>true there for another type of scope, plus I hear it might help. It doesn't make a difference.

I don't need all the comments of all Guesses. I want the top 3 (or 5). I am also trying to keep the one-liner call intact and simple, manage everything through scopes, relations and parameterized functions so that the API call itself is clean and simple.

Any advice?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql