Zend_Db嵌套查询,其中一个查询需要另一个查询的ID

时间:2012-10-10 14:23:31

标签: php mysql zend-framework zend-db-select

我是Zend Framework的新手,并且在没有找到以下问题的示例的情况下搜索了几天:

我试图通过下面的代码实现的结果是一个列表,它将具有主要类别,其下面有匹配的子类别,并循环遍历所有主要类别。

示例:

家电(主要类别)

  • 微波
  • 火炉

电子(主要类别)

  • 计算机
  • 无线电

以下是代码(我再次不知道如何执行此操作;我的思维过程是执行嵌套foreach(),第二个foreach()从第一个选择中获取主类别ID:

// Get  Categories with Sub Categories

$catid = 0;

$selectmain = $this->dbhInstance->select()
                                ->from(array('a' => 'code_sub_category'), 
                                       array('b.id as mainid', 'b.site_category'))
                                ->join(array('b' => 'site_categories'), 
                                       'b.id = a.site_category_id')
                                ->group("b.id")
                                ->order("b.site_category");

$selectsub = $this->dbhInstance->select()
                               ->from(array('a' => 'code_sub_category'), 
                                      array('a.id as subid', 'a.sub_category', 'a.site_category_id'))
                               ->join(array('b' => 'site_categories'), 
                                      'b.id = a.site_category_id')
                               ->where("a.site_category_id = '" . $catid . "'")
                               ->order("a.sub_category");

$fetch = $this->dbhInstance->fetchAll($selectmain);
$fetch2 = $this->dbhInstance->fetchAll($selectsub);
//var_dump($fetch2);

$items = array();
$items2 = array();

foreach ($fetch as $key => $value) {
    $catid = $value['id'];
    $items = array_merge($items, array($key => $value));

    foreach ($fetch2 as $key => $value) {
        $items = array_merge($items, array($key => $value));
    }

    $this->view->getsubcategories = $items;
}

$this->view->getmaincategories = $items;
//End FULL Categories for My Categories

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

$selectCat = $this->dbhInstance->select()
                            ->from(array('c' => 'site_categories'), array('c.id as id', 'c.site_category as text, 0 as parent'))
                            ->order('c.site_category');

$selectSubCat = $this->dbhInstance->select()
                            ->from(array('sc' => 'code_sub_category'), array('sc.id as id', 'sc.sub_category as text', 'sc.site_category_id as parent')
                            ->order('sc.sub_category');

$select =   $this->dbhInstance->select()->union(array($selectCat, $selectSubCat));

$statement = $this->dbhInstance->query($select);

$items = array();
$childs = array();

while( $row = $statement->fetch() ) {
     $items[] = (object)array(
    'id' => $row['id'],
    'text' => $row['text'],
    'parent' => $row['parent'],
    'item' => array()
);
}

foreach($items as $item)
$childs[$item->parent][] = $item;

foreach($items as $item) if (isset($childs[$item->id]))
$item->item = $childs[$item->id];

if( !isset($childs[0]) ) $this->view->getmaincategories = $getmaincategories = array();
else 
$this->view->getmaincategories = $getmaincategories = $childs[0];

// Display the result 
Zend_Debug::dump($getmaincategories);

结果将是一个对象数组,如下所示:

array(
'Appliances' => array(
    'Microwave',
    'Stove'
),
'Electronics' => array(
    'Computer',
    'Radio'
)
)

希望这有帮助!

此致 艾哈迈德。