Zf2 \ Zend \ Db \ Sql \ Sql using a predicate in which the condition

I really don't understand how to use predicates in zend framework 2.

here is what i got:

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

it works fine

Now I want to apply something like this:

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

This is what I tried, it does not cause errors, but it does not work: - (

This is what I expect from SQL:

select GroupedColum
    , min(ValueColumn) as minValue
from ValueTable
where filterColumn > 20
group by GroupedColum
order by minValue
    GroupedColum
limit 10;
+5
source share
2 answers

I solved the problem by looking at the source code zf2

If you know how to do this, it’s very simple, the code above was almost right!

Instead

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

you should use

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

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

this is a 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'));
+9
source

A bit late, but you can also accomplish this by doing

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

__get \Zend\Db\Sql\Select , where(), ,

$sql->where
    ->greaterThan('filterColumn', '20')
    ->or
    ->greaterThan('filterColumn', '30');

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

, ZF 2 (2.1.0), Where:

+6

All Articles