Zend Framework 2 Sql Select with OR and AND

后端 未结 2 685
囚心锁ツ
囚心锁ツ 2020-12-30 10:49

I want to make this query using Zend\\Db\\Sql\\Select:

SELECT table1.* FROM table1 
    INNER JOIN table2 ON table1.columnA = table2.columnB 
    INNER JOIN          


        
相关标签:
2条回答
  • 2020-12-30 11:09

    from the top of the head using Where fluent interface:

    $select->where
           ->nest
               ->equalTo('table2.column2', 2)
               ->or
               ->equalTo('table2.column3', 3)
           ->unnest
           ->and
           ->equalTo('table1.column1', 1);
    
    0 讨论(0)
  • 2020-12-30 11:14

    I would do something like:

    $where = new \Zend\Db\Sql\Where();
    
    $where
        ->nest()
        ->equalTo('table2.column2', 2)
        ->or
        ->equalTo('table2.column3', 3)
        ->unnest()
        ->and
        ->equalTo('table1.column1', 1);
    $select->where($where)
    

    Just because this way your $select keep being an implementation of Zend\Db\Sql\SqlInterface while doing

    $select->where
       ->nest
    

    will return an instance of a Zend Sql operator. Which is not bad but then you can't just do

    $statement = $sql->prepareStatementForSqlObject($select);
    $resultSet = $statement->execute();
    
    0 讨论(0)
提交回复
热议问题