如何在“where”部分写自定义“或”条件; zf2 ZendFramework

时间:2014-04-03 04:34:06

标签: php mysql zend-framework zend-framework2

我正在使用以下查询

$this->tableGateway->getSql()
    ->select()
    ->join(array('pu'=>'project_users'), 'users.id = pu.user_id', array('project_id'), 'left')
    ->join(array('r'=>'roles'), 'pu.role_id=r.id', array('role'), 'left')
    ->where(array('company_id'=>$company_id,'users.status'=>'1',''));

我需要的是:

将条件应用于以下

where  company_id=1 and users.status='1' and (project_id=null or project_id=1)

我无法得到这个

请帮忙

2 个答案:

答案 0 :(得分:1)

试试这个,最后一行:

$this->tableGateway->getSql()->select()
        ->join(array('pu'=>'project_users'), 'users.id = pu.user_id', array('project_id'), 'left')
        ->join(array('r'=>'roles'), 'pu.role_id=r.id', array('role'), 'left')
        ->where(array('company_id'=>$company_id,'users.status'=>'1'))
        ->where(array('project_id'=>null,'project_id'=>'1'), Predicate\PredicateSet::OP_OR);

答案 1 :(得分:0)

我做的就像

$where = new \Zend\Db\Sql\Where();
$where->equalTo('company_id',$company_id)->and
->equalTo('users.status','1')->nest()
->isNull('project_id')->or->equalTo('project_id',$project_id)
->unnest();         

$resultSet = $this->tableGateway->getSql()->select()
->join(array('pu'=>'project_users'), 'users.id = pu.user_id', 
array('project_id'), 'left')
->join(array('r'=>'roles'),'pu.role_id=r.id', array('role'),'left')
->where($where);