帖子和评论存储在同一个表格中。因此,为了获得每篇文章及其评论,我们这样做:
$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
...
)
)
)
对其他解决方案的任何提示也非常受欢迎。
感谢。
答案 0 :(得分:1)
如果您将第二个连接中的所有列别名发布到帖子(如idPost作为child_idPost ...等),您将获得许多行,这些行是包含第二行列的父行。多数民众赞成你最接近的。然后,您可以从第一行获取父数据,然后遍历后续行以获取一对多数据。
否则,只需进行两次查询,一次针对父级,一种针对子级。它可能比创建大型结果表更快。
答案 1 :(得分: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类。根据您的数据库,信息可能来自:
遗憾的是,其他适配器可能无法获取表名。
答案 2 :(得分: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