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

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

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

谢谢。

有帮助吗?

解决方案

如果你的别名都在第二列加盟职位(如idPost作为child_idPost ...等),你会得到与第二行的列父行多行。你会得到最接近的那。然后,您可以通过后续行抢从第一行父数据,然后循环,让您一到多的数据。

否则,只是做两个查询,一个是父母,一个孩子。它可能比反正创建较大的结果表更快。

其他提示

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 类。根据您的数据库,信息可能来自:

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

是的,我们有同样的问题,刚做完博客看:的 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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top