问题
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