状況としては、UserモデルとStudentモデルがある。Userは複数のStudentを持つ。
さらに、Studentは複数のCourseを持つ。Courseは重複するのでHABTM Associationとして登録。
Studentは各コース毎に受講しているか休学してるかstatusで管理する。status = 1 -> 受講中 、status = 0 -> 休学とする。
受講、休学はこんなボタンを表示させて、それぞれ studentsコントローラのadmin_couse_take()で処理を行う。
ちなみに、コントローラ内のファンクションにこうやって複数引数を渡せることも知った。
// absent
echo $this->Form->postLink(__('Absent'), array(
'controller' => 'students',
'action' => 'couse_take',
$student['Student']['id'],
$course['CoursesStudent']['id'],
false,
'admin' => true
), null, __('Are you sure you want to retake the class?'));
// take
echo $this->Form->postLink(__('Take'), array(
'controller' => 'students',
'action' => 'couse_take',
$student['Student']['id'],
$course['CoursesStudent']['id'],
true,
'admin' => true
), null, __('Are you sure you want to retake the class?'));
admin_couse_take()内の処理。
$take = false のときはstatusを0に。
public function admin_couse_take($id = null, $CoursesStudent_id = null, $take = false) {
$this->Student->id = $id;
if (!$this->Student->exists()) {
throw new NotFoundException(__('Invalid student'));
}
if($take){
$mystatus = 1;
}else{
$mystatus = 0;
}
$change_status = array( "CoursesStudent.status" => $mystatus );
$conditions = array( "CoursesStudent.id" => $CoursesStudent_id );
if ($this->Student->CoursesStudent->updateAll($change_status, $conditions)) {
$this->Session->setFlash(__('The course status updated'),'default', array('class' => 'alert alert-warning fade in'));
$this->redirect($this->referer());
}
$this->Session->setFlash(__('The student could not be deleted. Please, try again.'));
// redirect to current page
$this->redirect($this->referer());
}
cakeは楽しいな~☆