tuupola / slim-api-skeleton

Slim 3 API skeleton project for Composer
MIT License
312 stars 62 forks source link

[QUESTION] How to add MySQL ORDER BY #41

Closed controlnocontrol closed 6 years ago

controlnocontrol commented 6 years ago

I'm looking at the zend-db documentation and they indicate that ORDER BY is achieved with something like:

$select = new Select;
$select->order('id DESC'); // produces 'id' DESC

Your ZendTodoRepository all function seems to always expect an array. Was your framework set up with ORDER BY in mind? How can we achieve this?

tuupola commented 6 years ago

It is not really a framework, more like a skeleton which can be used for starting new projects. Reading through Zend docs you are correct. TableGateway seems to require a Select object to pass in ORDER BY clause. One way to achieve this would be to alter the all() method in ZendTodoRepository. Something like:

    public function all(array $specification = []): array
    {
        $select = /* Build $select from $specification here. */
        $rowset = $this->table->selectWith($select); 
        return map($rowset, function ($row) {
            return $this->hydrator->hydrate((array) $row, new Todo);
        });
    }
controlnocontrol commented 6 years ago

Ok awesome that's what I'll be doing. Was just wondering whether you had that covered. Thanks!

tuupola commented 6 years ago

I just recently converted this to Zend TableGateway and have not needed an ORDER BY query yet by myself. But since it is common use case I should find a good way to do it. Or at least mention this in docs.

Thanks for the heads up!