duanlu6114 2012-08-11 06:58
浏览 35
已采纳

从两个连接表中检索所有投票

So I'm linking two of my models together and it's returning something like:

Array
(
    [Submission] => Array
        (
            [id] => 47
            [user_id] => 0
            [title] => asdfasdfsa dfasdf asdfa sfa fadf
            [source] => http://www.aol.com
            [slug] => 
            [category] => health
            [created] => 2012-06-25 11:30:16
        )

    [User] => Array
        (
            [id] => 2
            [username] => john
        )

    [SubmissionsVote] => Array
        (
            [0] => Array
                (
                    [id] => 247
                    [user_id] => 2
                    [submission_id] => 47
                    [vote_type] => up
                )

        )

)

Sometimes, however, the number of votes will be from [0] - [n]. This is for my $newestSubmissions. The query I'm doing to give me that is:

public function newestSubmissions() {
    $this->unBindModel(
            array('hasMany' => array('Comment')));
    return $this->find('all', array(
                'fields' => array(
                    'Submission.id',
                    'Submission.user_id',
                    'Submission.title',
                    'Submission.source',
                    'Submission.slug',
                    'Submission.category',
                    'Submission.created',
                    'User.id',
                    'User.username'
                ),
                'order' => 'Submission.created DESC'
            ));
}

What I'd like to do is to retrieve all votes throughout the $newestSubmissions array object as well as all of the votes (query below) on each of the $newestSubmissions to calculate the actual score so I can send a single array object called $newestSubmissions to my view, instead of $newestSubmissions and $submissionScore

public function getVoteType($userId, $submissionId) {
    $voteType = $this->find('all', array(
        'conditions' => array(
            'User.id' => $userId,
            'Submission.id' => $submissionId),
        'fields' => array(
            'SubmissionsVote.vote_type'),
        'limit' => '1'
            ));
    return $voteType[0]['SubmissionsVote']['vote_type'];
}

Basically I want to send a single score to my view along with all the other information in the array object instead of having to send an inner indexed array object within my parent array object (which is what I'm currently getting).

How can I do this?

  • 写回答

2条回答 默认 最新

  • doute7910 2012-08-11 08:36
    关注

    OK... I tried to post this just as a comment, but it was too long. It's not really an answer as such.

    Do you know about the containable behaviour? Check it out - it's good for determining what data you get back: http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

    Also, if you were to break your submissions vote table into two tables - upvotes and downvotes, then you could also use Cake's counterCache behaviour to keep track of the number of up/down votes directly in your 'submissions' table (you'd need to add upvote_count and downvote_count columns to your submissions table to do so).

    This would make calculating submission score easier - and you wouldn't have to fetch all votes from the database to do it each time. For doco, search for counterCache on this page: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html It could be worth considering.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错