2009-07-22 45 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类,可能通过提供您自己的适配器类。根据您的数据库,该信息可能来自:

其他适配器可能没有办法获得表名,很遗憾。