zf2 \Zend\Db\Sql\Sql using predicate in where condition

RomanKonz picture RomanKonz · Sep 26, 2012 · Viewed 14.1k times · Source

i really don't get the point how to use predicates in zend framework 2.

this is what i got:

$sql->select()
        ->columns(array('GroupedColum'
            ,'minValue' => new Expression('min(ValueColumn)')))
        ->from('ValueTable')
        ->group('GroupedColum')
        ->order('minValue')
        ->order('GroupedColum')
        ->limit(10);

this is working fine

now i want to apply somethin like that:

$predicate = new Zend\Db\Sql\Predicate\Predicate();
$sql->where($predicate->greaterThan('filterColumn','20);

this is what i tried, it throws no error, but it is not working :-(

This is what i expect as SQL:

select GroupedColum
    , min(ValueColumn) as minValue
from ValueTable
where filterColumn > 20
group by GroupedColum
order by minValue
    GroupedColum
limit 10;

Answer

RomanKonz picture RomanKonz · Sep 27, 2012

I've solved the problem by viewing the source code of zf2

If you know how to do it, it is really easy, the code above was almost right!

Instead of

$predicate = new Zend\Db\Sql\Predicate\Predicate();

you have to use

$predicate = new  \Zend\Db\Sql\Where();

it is an empty derived class, that is used in Zend\Db\Sql\Sql

this is the complete working example:

$sql->select()
    ->columns(array('GroupedColum'
        ,'minValue' => new Expression('min(ValueColumn)')))
    ->from('ValueTable')
    ->group('GroupedColum')
    ->order('minValue')
    ->order('GroupedColum')
    ->limit(10);
$predicate = new  \Zend\Db\Sql\Where();
$sql->where($predicate->greaterThan('filterColumn','20'));