今次開 db 目標係 cross check 新 table 有冇資料
1. 開新 database table 其實同平時無乜分別
係 table 名要有 s
例如 students
2. 開 Model file 就跟 database table 名, 不過無s
\app\models\student.php
<?php
class Student extends AppModel {
var $name = 'Student';
}
?>
CakePHP 會自動追番 students 個 table
3. 做validate
今次開 db 目標係 cross check 新 table 有冇資料
係原本既 Model 加多個 function
public function check_exist($check){
$this->bindModel(
array(
'belongsTo' => array(
'Student' => array(
'className' => 'Student',
'foreignKey' => 'id'
)
)
), false
);
$this->Student->recursive = -1;
$result = $this->Student->find(
'first', array(
'conditions' => array(
'Student.eid' => $check
),
'fields' => array('Student.eid')
)
);
if( $result != false) {
return true;
}else {
return false;
}
}
咁就做完
一句SQL 都唔洗
都幾爽
係要睇下d doc
http://book.cakephp.org/1.3/en/view/1001/Understanding-Models
http://book.cakephp.org/1.3/en/view/1039/Associations-Linking-Models-Together