2012-02-23 90 views
0

我试图通过查看用户是否可以注册课程来运行。我有2个问题:CakePHP:允许用户注册课程

  1. 我正在以正确的方式进行吗?
  2. 我的学生:: isSignedUpForCourse()函数没有返回正确的课程(它实际上返回2门课程)。我怎样才能将它在课程模型中被使用的课程(课程:: CanSignupForCourse)发送给它?

谢谢!

// 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()); 
} 

回答

0

在模型中,您指的是模型。你只需要参考$ this,而不是$ this-> Student。

public function canSignupForCourse($studentId) { 
    $this->Student->id = $studentId; 

应该

public function canSignupForCourse($studentId) { 
    $this->id = $studentId; 

UPDATE

更糟的是,你不能重载一个模型一样,另一种模式。如果没有实例化模型,则无法从另一个模型中调用模型。您需要在引用学生模型之前添加此项:

App :: uses('Student','Model'); $ student = new Student(); $ student-> id = $ student_id;

但是,您的功能的设计应该更新。您可能应该将学号和课程编号传递给$this->Student->isSignedUpForCourse();。你现在的方式使得代码难以阅读和理解。我想改变isSignedUpForCourse功能如下:

public function isSignedUpForCourse($student_id = null, $course_id = null) { 
    if (!$student_id or !$course_id) { 
     return false; 
    } 
    // access the model to determine course/student 
} 

随着出会心当然/学生之间的关系(虽然我认为它是HABTM),我不能提供合适的课程/学生检测代码。但我认为你明白了。

+0

这是课程模型。 – execv 2012-02-23 01:18:38

+0

看到我上面的更新。 – 2012-02-23 03:31:42

相关问题