Zend 框架 2:获取有序的 SQL 调用 [英] Zend Framework 2: Getting an ordered SQL call

查看:22
本文介绍了Zend 框架 2:获取有序的 SQL 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获得一个字段的订单 ASC/DESC 调用(假设已创建),但我似乎不知道如何在 ZF2 中执行此操作.

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

推荐答案

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

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

通过条件/Where的示例

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

这篇关于Zend 框架 2:获取有序的 SQL 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆