Zend Framework Select Objects And UNION()

前端 未结 6 1984
南旧
南旧 2020-12-30 16:16

I\'m pretty sure this is not possible in Zend Framework (I have searched the Web, the documentation and issue tracker) but I just want to make sure so I\'m asking here.

相关标签:
6条回答
  • 2020-12-30 16:59

    Here's what I've done to make a union:

    $select = $this->select();
    //common select from both sides of the union goes here
    
    $select1 = clone($select);
    //select1 specifics here
    
    $select2 = clone($select);
    //select 2 specifics here
    
    $db = $this->getAdapter();
    $pageselect = $db->select()->union(array("($select1)", "($select2)"));
    

    Remember Db_Select's __toString will print out the SQL generated by that select, to help you debug.

    0 讨论(0)
  • 2020-12-30 17:15

    This practical example shows a function that returns a rowset of either latest or if a available favourite blog entries of a specific year (artwork blog):

    public function fetchBestOf($year)
    {
        $selectLatest = $this->select()->where('isHidden = 0')
                                       ->where('YEAR(dateCreated) = ' . $year)
                                       ->where('isHighlight = 0');
        $selectHighlights = $this->select()->where('isHidden = 0')
                                           ->where('YEAR(dateCreated) = ' . $year)
                                           ->where('isHighlight = 1');
    
        $selectUnion = $this->select()->union(array($selectLatest, $selectHighlights), Zend_Db_Select::SQL_UNION_ALL)
                       ->order('isHighlight DESC')
                       ->order('dateCreated DESC')
                       ->order('workID DESC')
                       ->limit('5');
    
        $rowset = $this->fetchAll($selectUnion);
        return $rowset;
    }
    
    0 讨论(0)
  • 2020-12-30 17:16

    Zend_Db_Select has a union method so I'd have thought it is possible, if you can build your query using a select object. I haven't used Zend_Db_Select (or the table subclass) with union but I'd imagine you can do something like

    $select = $this->select()
                   ->where('blah')
                   ->union($sql);
    
    0 讨论(0)
  • 2020-12-30 17:16

    The best way Zend suggest is like follows....

    $sql = $this->_db->select()
        ->union(array($select1, $select2,$select3))
                ->order('by_someorder');
    
    echo $sql->__toString();
    
    $stmt = $db->query($sql);
    $result = $stmt->fetchAll();
    

    echo will show the query

    Here $select1, $select2, $select3 can be different select queries with same number of columns...

    0 讨论(0)
  • 2020-12-30 17:18

    This is how it works for me:

    $select1 = $this->select();               
    $select2 = $this->select();
    

    After getting the necessary data in both queries the UNION syntax goes like this:

     $select = $this->select()->union(array('('.$select1.')', '('.$select2.')'));  
    
    0 讨论(0)
  • 2020-12-30 17:22

    a complete example:

     public function getReservationById($id)
     {
      if(!$id) return null;
    
      $sql = $this->table->select();
      $sql->union(array(
       $this->table->select()->where('id=?', $id),
       $this->tableFinished->select()->where('id=?', $id),
       $this->tableCanceled->select()->where('id=?', $id),
       $this->tableTrashed->select()->where('id=?', $id)
       ));
      echo $sql->__toString();
     }
    

    and the generated query:

    SELECT reservations.* FROM reservations WHERE (id='5658') UNION SELECT res_finished.* FROM res_finished WHERE (id='5658') UNION SELECT res_cancel.* FROM res_cancel WHERE (id='5658') UNION SELECT res_trash.* FROM res_trash WHERE (id='5658')

    0 讨论(0)
提交回复
热议问题