How todo a orWhere in ZF2?

江枫思渺然 提交于 2019-12-12 18:07:58

问题


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);


来源:https://stackoverflow.com/questions/34845452/how-todo-a-orwhere-in-zf2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!