aserwin
aserwin

Reputation: 1050

Zend 2 TableGateway conundrum?

I have these 2 pieces of code... In MY head, they are identical, but one works and one doesn't. Hoping someone can help me figure this out.

This code doesn't work. It returns the entire table, ignoring the orderby and the limit.

public function getNews($limit = null)
{
    $select = new Select();

    $select->order('Date DESC');
    if($limit != null)
    {
        $select->limit($limit);
    }

    $result = $this->gateway->select($select);
    return $result;
}

This code is rearranged to use an anonymous function and works perfectly.

public function getNews($limit = null)
{
    $result = $this->gateway->select(
        function(Select $select) use ($limit)
        {
            $select->order('Date DESC');
            if($limit != null)
            {
                $select->limit($limit);
            }
        }
    );

    return $result;
}

Any insight would be appreciated.

Upvotes: 0

Views: 154

Answers (1)

radnan
radnan

Reputation: 1289

The second way is how the TableGateway::select method is meant to be used. You either pass it a simple array of where predicates or a Closure that then performs more complex operations on the Select object.

Check out the documentation on TableGateway for more details.

Upvotes: 2

Related Questions