2012-02-23 192 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

相關問題