Zend Framework 2: Getting an ordered SQL call

 ̄綄美尐妖づ 提交于 2019-12-01 23:23:18

问题


I've been trying to get an order ASC/DESC call for a field (let's say craeted), and I can't seem to figure out how to do it in ZF2.

Where am I going wrong..?

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

回答1:


You can use a closure to manipulate the Select object like so:

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

An example passing through conditions / 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;
}


来源:https://stackoverflow.com/questions/16577658/zend-framework-2-getting-an-ordered-sql-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!