Zend框架选择对象和UNION()

时间:2009-08-23 21:29:06

标签: php mysql zend-framework select union

我很确定这在Zend Framework中是不可能的(我已经搜索过网络,文档和问题跟踪器),但我只是想确定一下,我在这里问。

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

当然不行。解释我需要什么。我需要使用UNION()在SELECT查询中合并2个表,我知道我可以这样做:

$select = "$select1 UNION $select2";

问题是会返回一个字符串,我需要一个select对象,所以我可以将它与Zend_Paginator一起使用。

我已经通过修改我的数据库架构解决了这个问题,但我很好奇是否有一些解决方法。

6 个答案:

答案 0 :(得分:13)

以下是我为建立工会所做的工作:

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

请记住Db_Select的{​​{1}}将打印出该选择生成的SQL,以帮助您进行调试。

答案 1 :(得分:7)

Zend_Db_Select has a union method所以我认为如果你可以使用select对象构建查询是可能的。我没有使用Zend_Db_Select(或表子类)与union,但我想你可以做类似的事情

$select = $this->select()
               ->where('blah')
               ->union($sql);

答案 2 :(得分:4)

一个完整的例子:

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

和生成的查询:

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

答案 3 :(得分:3)

此实际示例显示了一个函数,该函数返回最新的行集或特定年份的可用收藏博客条目(艺术作品博客):

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

答案 4 :(得分:2)

  

Zend建议的最佳方式如下......

$sql = $this->_db->select()
    ->union(array($select1, $select2,$select3))
            ->order('by_someorder');

echo $sql->__toString();

$stmt = $db->query($sql);
$result = $stmt->fetchAll();
  

echo将显示查询

     

这里$ select1,$ select2,$ select3可以是不同的select查询   列数......

答案 5 :(得分:1)

这就是我的工作方式:

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

在两个查询中获取必要的数据之后,UNION语法如下所示:

 $select = $this->select()->union(array('('.$select1.')', '('.$select2.')'));