小池有话说

Zend Sql Object 进阶

2014-01-23

to get LIKE

WHERE foo LIKE '%bar%'

you need

<?php
use Zend\Db\Sql\Predicate;
$sql->where(new Predicate\Like('foo', '%bar%'));

to get OR

WHERE (foo IS NULL OR bar IN (1, 2, 3)) AND baz=4

you need

<?php
use Zend\Db\Sql\Predicate;
$pred = new Predicate(array(
    new Predicate\IsNull('foo'),
    new Predicate\In('bar', array(1, 2, 3))),
    PredicateSet::OP_OR
);
$sql->where($pred)->where(array('baz' => 4));

to get COUNT

select COUNT(*) FROM foo

you need

<?php
$sql->from('foo')->count();

to get COUNT in column

SELECT COUNT(id) AS c, name AS name FROM foo GROUP BY name

you need

<?php
$sql->from('foo')
    ->columns(array(
        'c' => new Expression('COUNT(id)')),
        'name' => 'name',
    )
    ->group(array('name'))
    ;