Sorting related models in hasMany

Hey community!

I have a model with a hasMany-related model. Now I want to sort the results in the hasMany relation.

Example: I have a "Posts"-Controller and a "Comments"-Controller. The comments to a post should be sorted by date DESC. How can I do that with models?

greetings Eike

Eike

For ORM you would need to use the magic method. Here is how you could do it:

        $grabPosts = Posts::findFirst(2);

        foreach ($grabPosts->getComments(array("order"=>"comment_id DESC")) as $comment) {
            echo $comment->getCommentId();
        }

Doing it in the model itself isn't complete yet, or at least not an easy way of handling this. There is a new feature request in github for automatically defining defaults in the model here: https://github.com/phalcon/cphalcon/issues/926



7.9k

HEY @Raistlfiren! Thanks for your reply. I thought I could do it in the way Agent-J wanted it in his reported issue. How did you find that issue so fast? Next time I search for an issue before asking here ;)

Eike,

If you search github under cphalcon/issues you can find a lot more new feature requests and other things similar to it. =)

Cheers!