Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Is it possible to order/sort a Phalcon\Mvc\Model\Resultset\Simple

I have retreived a Recordset. What's the best means to sort it? I can't see anything in the API documentation. I want to do something like:

$user = $this->getUser();
$projects = $user->projects->orderBy("created_at");

.. but instead I have to do:

// get the currently logged in user, and their projects
$user = $this->getUser();
$projects = Projects::query()
    ->where("user_id = :user_id:")
    ->bind( array("user_id" => $user->id) )
    ->orderBy('id')
    ->execute();

I think this is a bit of a hack, but I got this sort of thing working as follows:

        $projects = $user->getProjects(array("order" => "[\Namespace\Projects].[created_at]"));


53.3k
Accepted
answer

I think this is a bit of a hack, but I got this sort of thing working as follows:

```php $projects = $user->getProjects(array("order" => "[\Namespace\Projects].[created_at]")); ```

That's not a hack - that's the way you're supposed to do it I think, though I think you can simplify it to:

$projects = $user->getProjects(array("order"=>"created_at"));


30.7k

Hi,

I think that relations must have been declared in models before using the magic method "getProjects"