在循环通过具有多个连接的数组时创建更易于管理的数组的方法?

时间:2012-09-28 05:32:40

标签: php mysql sql cakephp join

我有一个数组,我传递给我的视图,它与多个表连接。我们称这个数组为Submissions。所有submissions都包含SubmissionVote个,典型数据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并抓住:$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'));

我想要做到这一点的唯一原因是因为在我的观点中经历并且不得不将所有upvotes和downvotes加起来会非常痛苦,更不用说相当邋。了。

我该如何改善这个?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用虚拟字段http://book.cakephp.org/2.0/en/models/virtual-fields.html

Submission模型中,您会遇到类似的内容:

public $virtualFields = array(
    'up_count' => 'SELECT COUNT(*) FROM submission_votes AS up_count WHERE submission_id = Submission.id AND vote_type = "up"',
    'down_count' => 'SELECT COUNT(*) FROM submission_votes AS down_count WHERE submission_id = Submission.id AND vote_type="down"',
);

在上面的示例中,submission_votes是您的SubmissionVote模型的数据库表名。务必注意警告:http://book.cakephp.org/2.0/en/models/virtual-fields.html#limitations-of-virtualfields

如果您只想操纵数据数组,另一个选项可能是使用Hash类中的内容,例如Hash::reduce

http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::reduce