0

帖子和评论存储在同一个表中。因此,要获取每个帖子及其评论,我们这样做:

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

对其他解决方案的任何提示也非常受欢迎。

谢谢。

4

3 回答 3

1

如果您将第二个连接中的所有列别名为帖子(例如 idPost 作为 child_idPost ...等),您将获得许多行,这些行是具有第二行列的父行。那是您将获得的最接近的值。然后,您可以从第一行获取父数据,然后遍历后续行以获取您的一对多数据。

否则,只需执行两个查询,一个针对父级,一个针对子级。无论如何,它可能比创建大型结果表要快。

于 2009-07-25T17:48:15.937 回答
0

Zend 不会使您的首选形式变得简单,但它可能是可能的。但是请注意,您要求数据库服务器做的工作比您实际想要的要多得多,因为每个评论的帖子和用户信息都是重复的。贾斯汀是正确的,第二个查询更容易并且可能更快。但是,我可以提供一些解决方案的指示。

Zend_Db::FETCH_NUM首先,考虑使用fetch 模式会得到什么:

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

不幸的是,其他适配器可能无法获取表名。

于 2009-07-25T21:49:49.350 回答
0

是的,我们遇到了同样的问题,刚刚写完博客:http ://www.kintek.com.au/blog/zend-db-and-joins/

您需要唯一地命名每个字段,这是 Zend 中的一个已知错误:http://framework.zend.com/issues/browse/ZF-6421?page=com.atlassian.jira.plugin.system.issuetabpanels: all- tabpanel

于 2012-07-12T07:27:18.303 回答