I’m trying to run through seeing if a user can signup for a course. I have 2 questions:
- Am I going about this the right way?
- My student::isSignedUpForCourse() function isn’t returning the proper course (it’s actually returning 2 courses). How can I send it the course that is being used in the course model where it is called (Course::CanSignupForCourse)?
Thanks!
// Course Model
public function isComplete() {
$course = $this->read(null);
if($course['Course']['completed'] != 0) {
return true;
}
return false;
}
public function canSignupForCourse($studentId) {
$this->Student->id = $studentId;
if (!$this->Student->exists()) {
throw new NotFoundException(__('Invalid student'));
}
$this->Student->isSignedUpForCourse();
//this will ultimately be:
//if(! $this->Student->isSignedUpForCourse && $this->isApproved()) {
// return
}
}
// Course Controller:
public function signup($id = null) {
$this->Course->id = $id;
if (!$this->Course->exists()) {
throw new NotFoundException(__('Invalid course'));
}
if($this->Course->canSignupForCourse($this->Auth->user('id'))) {
// can signup
}
}
// Student Model
public function isSignedUpForCourse() {
print_r($this->read());
}
Inside your model you are referring to the model. You only need to refer to $this, not $this->Student.
should be
UPDATE
Even worse, you cannot overload a Model with another model like that. You cannot call a model from inside another model without Instantiation of the model. You need to add this before referencing the Student Model:
App::uses(‘Student’, ‘Model’);
$student = new Student();
$student->id = $student_id;
However, the design of your functions should be updated. You probably should be passing the student id and the course id to
$this->Student->isSignedUpForCourse();. The way you have it now makes the code difficult to read and understand. I would change the isSignedUpForCourse function to this:With out knowing the relationship between course/student (although I would assume it is HABTM), I cannot provide the proper course/student detection code. But I think you get the point.