How todo a orWhere in ZF2?

2019-07-08 08:48发布

问题:

I'm having difficulties to create params array for ZF2 where I can do a where / or

I know that orWhere is removed as of ZF2 but there are workaround which I can't get working:

use Zend\Db\Sql\Predicate;
$params = array(
        'select' => implode(', ', array(
            '`user`.`id`',
            '`user`.`username`',
            '`user`.`firstname`',
            '`user`.`middlename`',
            '`user`.`lastname`',
            '`user`.`externalEmail`',
            '`user`.`email`',

        )),


         "where" => implode(
                new Predicate\PredicateSet(
                    array(
                            new Predicate\Like( '`user`.`email` = :email'),
                            new Predicate\Like( '`user`.`email2` = :email'),
                    ),
                    Predicate\PredicateSet::COMBINED_BY_OR
                )
            ), ...

I thought this was the way todo but it seems not.

回答1:

You can use the NEST special property to do complex querying:

use \Zend\Db\Sql\Where;
use \Zend\Db\Sql\Select;

$select = new Select();
$select->from('user');

$where = new Where();
$where->NEST
    ->equalTo('email', $email)
    ->OR
    ->equalTo('email2', $email)
->UNNEST;
$select->where($where);