我有 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)?
发布评论
评论(2)
我认为最好的办法是尝试将这些限制作为模型中的验证规则来实现。
根据您的描述,申请学生课程是通过创建新的
StudentCourse
来完成的,因此您应该尝试适应验证规则,例如: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:我首先查看手册中的示例: 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
然后,我会将这些函数放入模型的验证对象中,如下所示:
并像这样定义模型函数:
现在,每当您尝试保存或验证( ) 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 doesntbelongto
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:
And define the model functions like this:
Now whenever you try to save or validate() CourseMembership, the validate will return a descriptive error message if it is unsuccessful.