Zend 框架选择对象和 UNION()

发布于 2024-08-03 12:12:30 字数 407 浏览 4 评论 0原文

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

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

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

$select = "$select1 UNION $select2";

问题是它会返回一个字符串,我需要获取一个选择对象,以便我可以将它与 Zend_Paginator 一起使用。

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

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.

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

That doesn't work of course. To explain what I need. I need to use UNION() to merge 2 tables in a SELECT query, I know I could just do:

$select = "$select1 UNION $select2";

The problem is that would return a string and I need to get a select object so I can use it with Zend_Paginator.

I have already solved the issue by modifying my database architecture but I'm just curious if there is some workaround for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

天生の放荡 2024-08-10 12:12:31

以下是我为创建联合所做的操作:

$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__toString 将打印出该选择生成的 SQL,以帮助您进行调试。

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.

财迷小姐 2024-08-10 12:12:31

Zend_Db_Select 有一个 union 方法 所以我想如果您可以使用选择对象构建查询,这是可能的。我没有将 Zend_Db_Select (或表子类)与 union 一起使用,但我想你可以做类似的事情

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

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);
深府石板幽径 2024-08-10 12:12:31

完整的示例:

 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.* 来自 res_trash 地点 (id='5658')

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

药祭#氼 2024-08-10 12:12:31

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

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

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;
}
你是暖光i 2024-08-10 12:12:31

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可以是具有相同的不同的选择查询
列数...

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...

妄断弥空 2024-08-10 12:12:31

这就是它对我来说的工作方式:

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

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

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

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.')'));  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文