Zend公司选择自加入覆盖领域(Zend Select with self join overwri

2019-10-31 23:45发布

帖子和评论都存储在同一个表。 因此,要获得每个岗位和意见,我们这样做:

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

其它解决方案任何提示也非常欢迎。

谢谢。

Answer 1:

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

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



Answer 2:

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

  • PDO: PDOStatement->getColumnMeta
  • 库MySQLi: mysqli_fetch_field_direct

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



Answer 3:

是的,我们有同样的问题,刚做完博客看: http://www.kintek.com.au/blog/zend-db-and-joins/

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



文章来源: Zend Select with self join overwriting fields