CakePHP:在哪里放置此函数

发布于 2025-01-08 08:24:55 字数 569 浏览 2 评论 0 原文

我有 3 个模型:学生、课程和学生课程。课程“hasAndBelongsToMany”学生、学生“hasMany”课程和学生课程“belongsTo”学生和课程。在学生注册课程之前,我需要检查一些事情(即:课程是否已满,该学生过去是否参加过该课程等)。我可以处理函数内部的逻辑,但是我应该将该函数放在哪个模型下?还有,应该怎么称呼呢?我想到的一种方法是:

// Student Model
public function canSignupForCourse($studentId, $courseId) {
    // is the course full?
    // have they signed up before, etc
    // return either true or false
}

// Could it then be called anywhere as:
if($this->Student->canSignupForCourse($studentId, $courseId)) {
    // etc
}

或者,是否有更好/更简单的方法来做到这一点(并且,我是否需要每次都发送学生ID和课程ID)?

I have 3 models: Student, Course and StudentCourse. Course 'hasAndBelongsToMany' Student, Student 'hasMany' Course, and StudentCourse 'belongsTo' Student and Course. Before a student can signup for a course, I need to check a few things (ie: is the course full, has that student taken that course in the past, etc). I can handle the logic inside of the function, but which model should I place that function under? And, how should it be called? One way I thought of was:

// Student Model
public function canSignupForCourse($studentId, $courseId) {
    // is the course full?
    // have they signed up before, etc
    // return either true or false
}

// Could it then be called anywhere as:
if($this->Student->canSignupForCourse($studentId, $courseId)) {
    // etc
}

Or, is there a better/easier way to do it (and, do I need to send both the studentid and courseid each time)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倥絔 2025-01-15 08:24:55

我认为最好的办法是尝试将这些限制作为模型中的验证规则来实现。

根据您的描述,申请学生课程是通过创建新的 StudentCourse 来完成的,因此您应该尝试适应验证规则,例如:

// StudentCourse.php

$validate = array(
    'course_id' => array(
         'rule' => array('maxStudents', 30),
         'required' => true,
         'on' => 'create'
    )
)

function maxStudents($check, $max) {
    $count = $this->find('count', array(
        'conditions' => array('course_id' => $check['course_id']),
        'contain' => false
    ));
    return $count < $max;
}

I think the best thing to do is to try to implement these restrictions as validation rule in the model.

According to your description, applying a student for a course is done by creating a new StudentCourse, so that's where you should try to fit the validation rules, for example:

// StudentCourse.php

$validate = array(
    'course_id' => array(
         'rule' => array('maxStudents', 30),
         'required' => true,
         'on' => 'create'
    )
)

function maxStudents($check, $max) {
    $count = $this->find('count', array(
        'conditions' => array('course_id' => $check['course_id']),
        'contain' => false
    ));
    return $count < $max;
}
德意的啸 2025-01-15 08:24:55

我首先查看手册中的示例: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model

这应该让您相信您也应该将 Student 'hasAndBelongsToMany' 课程设置为(因为课程 has 学生,但学生在模型关系中不 belongto 课程),

然后您可以定义该关系模型类似于 CourseMembership(如上面的示例链接所示),

然后我会将 canSignupForCourse 函数放入该模型中。不过,我可能会将此函数分成几个单独的函数,例如 courseNotFull 和 courseNotTakenBefore

然后,我会将这些函数放入模型的验证对象中,如下所示:

public $validate = array( 
    'course_id' => array(
        'courseNotFull' => array( 
            'rule' => array('courseNotFull'), 
            'message' => "Course is full", 
        ), 
        'courseNotTakenBefore' => array(
           'rule' => array('courseNotTakenBefore'), 
            'message' => "Student has taken course before", 
        )
    )
); 

并像这样定义模型函数:

function courseNotFull() {
    $this->Course->id = $this->data[$this->alias]['course_id'];
    $course = $this->Course->read();

    return $course['Course']['isFull'];
}

function courseTakenBefore() {
    $this->Student->id = $this->data[$this->alias]['student_id'];
    $this->Course->id = $this->data[$this->alias]['course_id'];

    $course = $this->Student->Course->findById($this->Course->id);

    return $course;
}

现在,每当您尝试保存或验证( ) CourseMembership,如果验证不成功,将返回描述性错误消息。

I'd first check out the example in the manual here: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model

This should convince you that you should probably make Student 'hasAndBelongsToMany' course as well (since Course has student but student doesnt belongto course in your model relationships)

You can then define that relationship model as something like CourseMembership (as in the example link above)

I would then put canSignupForCourse function in that model. However I'd probably split this function up into a few separate ones, like courseNotFull and courseNotTakenBefore

I would then put these functions into the model's validate object like so:

public $validate = array( 
    'course_id' => array(
        'courseNotFull' => array( 
            'rule' => array('courseNotFull'), 
            'message' => "Course is full", 
        ), 
        'courseNotTakenBefore' => array(
           'rule' => array('courseNotTakenBefore'), 
            'message' => "Student has taken course before", 
        )
    )
); 

And define the model functions like this:

function courseNotFull() {
    $this->Course->id = $this->data[$this->alias]['course_id'];
    $course = $this->Course->read();

    return $course['Course']['isFull'];
}

function courseTakenBefore() {
    $this->Student->id = $this->data[$this->alias]['student_id'];
    $this->Course->id = $this->data[$this->alias]['course_id'];

    $course = $this->Student->Course->findById($this->Course->id);

    return $course;
}

Now whenever you try to save or validate() CourseMembership, the validate will return a descriptive error message if it is unsuccessful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文