2009-07-22 47 views
0

帖子和評論都存儲在同一個表。因此,要獲得每個崗位和意見,我們這樣做:Zend的選擇與自聯接覆蓋領域

$posts = $this->select()->setIntegrityCheck(false) 
         ->from(array('post' => 'Posts'), array('*')) 
         ->where('post.idGroup = ' . $idGroup) 
         ->where('post.idTopic IS NULL') 
         ->order('post.date DESC') 
         ->limit($resultsPerPage, $resultsPerPage * ($page - 1)) 
         ->joinLeft(array('user' => 'Users'), 'post.idUser = user.idUser', 
          array('idUser', 'fname', 'lname', 'profileUrl', 'photoUrl')) 
         ->joinLeft(array('comment' => 'Posts'), 'comment.idTopic = post.idPost') 
         ->query()->fetchAll(); 

的問題是,所產生的陣列是平坦和註釋數據將覆蓋後的數據,這是什麼是返回一個例子:

[1] => Array 
    (
     [idPost] => 13 
     [idTopic] => 11 
     [idGroup] => 1 
     [idUser] => 84 
     [postContent] => Hello my name is Mud. 
     [postUrl] => 13/hello-my-name-is-mud 
     [postVotes] => 
     [postScore] => 
     [date] => 2009-07-21 16:39:09 
     [fname] => John 
     [lname] => Doe 
     [profileUrl] => john-doe 
     [photoUrl] => uploads/userprofiles/0/84/pic84_14 
    ) 

我們所希望得到的結果是更多的東西是這樣的:

[1] => array(
      [post] => array(
       [0] => array(
        idPost => 12, 
        postContent => This is a post..., 
        idGroup => 1 
        ... 
       ) 
      ), 
      [user] => array(
       [0] => array(
        userName => JohnDoe 
        ... 
        ) 
       ), 
      [comments] => array(
       [0] => array(
        idPost => 15, 
        postContent => This is a comment..., 
        idGroup => 1 
        ... 
       ), 
       [1] => array(
        idPost => 17, 
        postContent => This is another comment..., 
        idGroup => 1 
        ... 
       ) 
      ) 
     ) 

任何提示其他解決方案也非常歡迎。

謝謝。

+1

SQL不喜歡的工作。您必須執行一系列查詢才能以該格式獲取數據。 – jason 2009-07-24 15:43:24

回答

1

如果你的別名都在第二列加盟職位(如idPost作爲child_idPost ...等),你會得到與第二行的列父行多行。那關於最接近你會得到。然後,您可以從第一行獲取父數據,然後循環訪問後續行以獲取您的一對多數據。

否則,只是做兩個查詢,一個是父母,一個孩子。無論如何,它可能比創建大型結果表更快。

+0

是別名似乎是防止重疊的唯一解決方案,我們最終得到的是一個循環,使得另一個查詢在每個帖子上獲得評論。因此,有10個帖子的頁面是11個或更多的查詢,這感覺是錯誤的。 – lasse 2009-07-27 10:34:00

0

的Zend不會使你的首選形式簡單,但它是可能的。但請注意,您要求數據庫服務器執行的工作量遠遠超過您的實際需求,因爲每條評論都會重複發佈帖子和用戶信息。賈斯汀是正確的,第二個查詢更容易,可能更快。但是,我可以提供一些解決方案的指導。

首先,考慮你會用得到什麼Zend_Db::FETCH_NUM提取模式:

Array(
    [0] => Array(
     [0] => 12 
     [1] => 
     [2] => 1 
     [3] => 84 
     [4] => This is a post..., 
     [5] => 12/this-is-a-post 
     [6] => 
     [7] => 
     [8] => 2009-07-21 16:39:09 
     [9] => 84 
     [10] => John 
     [11] => Doe 
     [12] => john-doe 
     [13] => uploads/userprofiles/0/84/pic84_14 
     [14] => 15 
     [15] => 12 
     [16] => 1 
     [17] => 79 
     [18] => This is a comment..., 
     [19] => 
     [20] => 
     [21] => 
     [22] => 2009-07-21 17:40:10 
    ), 
    [1] => Array(
     [0] => 12 
     [1] => 
     [2] => 1 
     [3] => 84 
     [4] => This is a post..., 
     [5] => 12/this-is-a-post 
     [6] => 
     [7] => 
     [8] => 2009-07-21 16:39:09 
     [9] => 84 
     [10] => John 
     [11] => Doe 
     [12] => john-doe 
     [13] => uploads/userprofiles/0/84/pic84_14 
     [14] => 17 
     [15] => 12 
     [16] => 1 
     [17] => 127 
     [18] => This is another comment..., 
     [19] => 
     [20] => 
     [21] => 
     [22] => 2009-07-20 10:31:26 
    ) 
) 

後來不知怎的,你必須拿出列號的映射表和列名:

Array(
    [0] => post.idPost 
    [1] => post.idTopic 
    [2] => post.idGroup 
    [3] => post.idUser 
    [4] => post.postContent 
    [5] => post.postUrl 
    [6] => post.postVotes 
    [7] => post.postScore 
    [8] => post.date 
    [9] => user.idUser 
    [10] => user.fname 
    [11] => user.lname 
    [12] => user.profileUrl 
    [13] => user.photoUrl 
    [14] => comment.idPost 
    [15] => comment.idTopic 
    [16] => comment.idGroup 
    [17] => comment.idUser 
    [18] => comment.postContent 
    [19] => comment.postUrl 
    [20] => comment.postVotes 
    [21] => comment.postScore 
    [22] => comment.date 
) 

這是它得到棘手,因爲該表部分強烈數據庫特定的接口,而並不總是可行的部分。由於它與結果集相關,適配器也將是獲取結果的最佳位置;這意味着黑客Zend類,可能通過提供您自己的適配器類。根據您的數據庫,該信息可能來自:

其他適配器可能沒有辦法獲得表名,很遺憾。