Zend Framework 2:获取有序的SQL调用

Yos*_*ssi 3 php zend-db-table zend-db zend-framework2

我一直在尝试为一个字段订购ASC / DESC呼叫(比如说craeted),但我似乎无法弄清楚如何在ZF2中做到这一点。

我在哪里错了..?

namespace Todo\Model;
class TodoTable extends AbstractTableGateway {
public function __construct(Adapter $adapter) {
    $this->adapter = $adapter;
    $this->resultSetPrototype = new ResultSet();
    $this->resultSetPrototype->setArrayObjectPrototype(new Todo());

    $this->initialize();
}
public function fetchAll() {
    $resultSet = $this->select(array('user_id'=>$this->user_id));
return $resultSet;
}
}
Run Code Online (Sandbox Code Playgroud)

And*_*rew 5

您可以使用闭包来操纵Select对象,如下所示:

public function fetchAll() 
{
    // The Select object will be passed to your Closure
    // before the query is executed.

    $resultSet = $this->select(function (Select $select) use () {
        //$select->columns(array('user_id', 'email', 'name')); 
        $select->order('name ASC'); 
    });

    return $resultSet;
}
Run Code Online (Sandbox Code Playgroud)

通过条件的示例/位置

public function fetchAll($someCondition) 
{
    // The Select object will be passed to your Closure
    // before the query is executed.

    $resultSet = $this->select(function (Select $select) use ($someCondition) {
        $select->columns(array('user_id', 'email', 'name')); 
        $select->order('name ASC'); 
        $select->where(array('user_id'=> $someCondition));
    });

    return $resultSet;
}
Run Code Online (Sandbox Code Playgroud)