每次用户查看提交时,我都会跟踪计数并将其绑定到会话:
提交控制器:
// 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
,所以我很困惑如何让它工作?