0

我有一个要传递给我的视图的数组,它与多个表连接。我们称之为数组Submissions。全部submissions包含SubmissionVotes,典型的数据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_typevote_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'));

我想这样做的唯一原因是因为在我看来,必须将所有赞成票和反对票加起来会很痛苦,更不用说马虎了。

我该如何改进呢?

4

1 回答 1

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_votesSubmissionVote模型的数据库表名。请务必注意警告: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

于 2012-09-28T07:05:50.927 回答