We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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]"));


125.8k
Accepted
answer

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]"));

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"));


43.9k

Hi,

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