我一直在尝试获取一个字段的 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;
}
关于php - Zend 框架 2 : Getting an ordered SQL call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16577658/