我有一个要传递给我的视图的数组,它与多个表连接。我们称之为数组Submissions
。全部submissions
包含SubmissionVote
s,典型的数据submission
对象如下所示:
[1] => Array
(
[Submission] => Array
(
[user_id] => 17
[title] => Yo mama so fat when she went to the movies she sat next to everyone
[source] => http://www.ahajokes.com/ym01.html
[slug] => yo-mama-so-fat-when-she-went-to-the-movies-she-sat-next
[category] => funny
[created] => 2012-09-26 21:00:35
[id] => 104
)
[User] => Array
(
[id] => 17
[username] => bob_cobb
)
[SubmissionsVote] => Array
(
[0] => Array
(
[id] => 323
[user_id] => 2
[submission_id] => 104
[vote_type] => up
[when] => 0000-00-00 00:00:00
[voted_ip] => 842844107
)
[1] => Array
(
[id] => 322
[user_id] => 17
[submission_id] => 104
[vote_type] => up
[when] => 0000-00-00 00:00:00
[voted_ip] => 1163843117
)
)
[SubmissionThumbnails] => Array
(
[0] => Array
(
[id] => 6
[submission_id] => 104
[original] => http://www.ahajokes.com/g/smlogo.gif
[extension] => jpg
[slug] => http://www.ahajokes.com/g/smlogo.gif
)
)
)
现在,在我看来,我只是在做这个,并从中foreach
获取:以及其他有用的东西。这一切都很好,但我希望能够以某种方式制作一个具有 up和 down数量的新对象(或同样直观的东西),这样我就可以像/ 。$submission['Submission']['id']
$submission['Submission']['category']
vote_type
vote_type
$submission['Submission']['upVotes']
$submission['Submission']['downVotes']
我正在 CakePHP 中加入,这是我的查询:
$this->find('all', array(
'fields' => array(
'Submission.user_id',
'Submission.title',
'Submission.source',
'Submission.slug',
'Submission.category',
'Submission.created',
'User.id',
'User.username'),
'order' => 'Submission.created DESC'));
我想这样做的唯一原因是因为在我看来,必须将所有赞成票和反对票加起来会很痛苦,更不用说马虎了。
我该如何改进呢?