という名前の db テーブルがStudent
あり、列はid
, name
,age
です。
id
は主キーでname
あり、null ではなく、null にage
することができます
PHP、Zendで次を実装したい:
値が異なる場合にのみ、dbに重複した学生を追加します。name
age
function/action 内でaddStudentAction
、 Student モデルの関数を呼び出してfindStudent
、要件を実装します。
public function findStudent($name)
{
$result = null;
$select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
->setIntegrityCheck(false)
->where('student.name = ?', $name);
//error_log($select->assemble());
$result = $this->getAdapter()->fetchRow($select);
return $result;
}
public function addStudentAction() {
$data = $_POST;
$studentModel = new Application_Model_Student;
$result = $studentModel->findStudent($data['name']);
if(!empty($result)) {
error_log("found student name in db, going to get age");
//Check for age
if( (!empty($result['age'] )) {
if( $result['age'] != $data['age']) {
error_log("going to add student as new record");
return $this->insert($data);
}
else {
error_log("not adding new student record ");
}
}
}
}
ここで、値は同じだが値が異なる複数の学生のレコードが存在する可能性があります。name
age
したがって、新しい学生を追加するときは、すべてのレコードの年齢 (名前が一致する場合) を年齢の受信値と比較する必要があります。
この要件を実装する最善の方法は何ですか? どんな助けでも大歓迎です。
ありがとう