ZF2 tableGateway select

user458753 picture user458753 · Apr 24, 2012 · Viewed 11.1k times · Source

I started with the ZendSkeletonApplication and added a model extending Zend\Db\TableGateway\TableGateway. I have the following method:

public function findByType($type) {
    $rowset = $this->select('type' => $type);
    return $rowset;
}

This works, but now if i do this:

$foo = $table->findBytype('foo');
$bar = $table->findBytype('bar');

the first one works, the query it executes is:

SELECT * FROM table WHERE 'type' = 'foo'

The second one however executes the following query:

SELECT * FROM table WHERE 'type' = 'foo' AND 'type' = 'bar'

is this expected behavior? If so how can i have the second time i call the method execute the following query:

SELECT * FROM table WHERE 'type' = 'bar'

thanks in advance!

Answer

AlloVince picture AlloVince · Jun 9, 2012

Should use select in tableGateway like this:

$select = $this->getSql()->select();
$select->where(array('type' => 'foo'))
    ->where(array('type' => 'bar'));
$rowset = $this->selectWith($select);

select() will be reset the where() paramters when you call it next time.

See more usage in my blog: http://avnpc.com/pages/advanced-database-select-usage-in-zf2