数据库
首页 > 数据库> > php – Zend Framework 2:获取有序的SQL调用

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

作者:互联网

我一直试图获得一个ASC / DESC命令的字段(让我们说是疯狂),我似乎无法弄清楚如何在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;
}
}

解决方法:

您可以使用闭包操作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;
}

通过条件/ Where的一个例子

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;
}

标签:zend-db,zend-db-table,php,zend-framework2
来源: https://codeday.me/bug/20190831/1779334.html