0

每次用户查看提交时,我都会跟踪计数并将其绑定到会话:

提交控制器:

// Count view
if ($this->Session->check('viewed_submission_' . $submissionId) !== true) {
    $clientIp = ip2long($this->request->clientIp());
    $this->SubmissionsViews->countView($submissionId, $clientIp);
    $this->Session->write('viewed_submission_' . $submissionId, true);
}

我在 SubmissionsViews 表中跟踪它们。

提交视图模型:

class SubmissionsViews extends AppModel {

    var $name = 'SubmissionsViews';

    var $belongsTo = array(
        'Submission' => array(
            'className' => 'Submission'
        )   
    );

    public function countView($submissionId, $clientIp) {
        $this->set('submission_id', $submissionId);
        $this->set('user_ip', $clientIp);
        $this->save($this->data);
    }   
}

我的 SubmissionsView 表submissions_views具有以下字段:

  • ID
  • 提交ID
  • 用户IP
  • 创建

我正在尝试设置 counterCache 以跟踪该表的添加,但不知道如何设置它。我目前$belongsTo在我的提交模型中添加 counterCache:

class Submission extends AppModel {
    var $belongsTo = array(
        'User' => array(
            'className' => 'User'
        ),
        'SubmissionsViews' => array(
            'counterCache' => true
        )   
    ); 

但它告诉我它找不到Submission.submissions_views_id。在文档中,它只是说我需要在我的submissions表中添加一个名为的字段submissions_views_count,所以我很困惑如何让它工作?

4

1 回答 1

1
  1. 模型名称应该是单数的,所以SubmissionViews应该是SubmissionView.
  2. 您的关联不正确。SubmissionhasMany SubmissionView,而不是 belongsTo。
  3. counterCache 需要在SubmissionViewbelongsTo 配置中的模型中指定Submission,而不是在Submission模型文件中。请更仔细地阅读手册。
于 2012-10-14T13:30:50.620 回答