Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2: Getting an ordered SQL call

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;
}
}
like image 987
Yossi Avatar asked Sep 19 '26 12:09

Yossi


1 Answers

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;
}
like image 189
Andrew Avatar answered Sep 22 '26 01:09

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!