每次用户查看提交时,我都会跟踪计数并将其与会话联系起来:
SubmissionsController:
// 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表中跟踪它们。
SubmissionsViews Model:
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
包含以下字段:
我正在尝试设置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
的字段,所以我很困惑如何使其工作?
答案 0 :(得分:1)
SubmissionViews
应为SubmissionView
。Submission
hasMany SubmissionView
,而不是belongsTo。SubmissionView
的belongsTo配置内的Submission
模型中指定counterCache,而不是在Submission
模型文件中。请仔细阅读本手册。