Search code examples
phpdatabasezend-frameworkzend-db

Trouble ordering information when pulled from the database in zend.


So I am having trouble getting the information I pull from the database order according to the id in my database. I wrote the following function based on a tutorial in a book ('Pro Zend Framework Techniques' published by Apress) and the book is riddled with typos and mistakes so i'm hoping it is something im just overlooking.

    public function getRecentArticles ($count = 99, $namespace = 'article')
{
    $select = $this->select();
    $select->order = 'id DESC';
    $select->where('namespace = ?', $namespace);
    $select->limit($count);
    $results = $this->fetchAll($select);
    if ($results->count() > 0) {
        $articles = array();
        foreach ($results as $result) {
            $articles[$result->id] = new Rt_Content_Item_Article($result->id);
        }
        return $articles;
    } else {
        return null;
    }
}

As you can see, I am trying to arrange the articles in descending order based on the ID field in the database. Any advice would be great. Thanks.


Solution

  • order is a function, same as where and limit. So your order line should be:

    $select->order('id DESC');