A Flexible Method of Storing Control Data
By Tony Marston
2006-08-29
From screen to database
After the user has changed any values he presses the "submit" button to send those changes to the server for processing. Everyone knows that user input should never be trusted, and should be "cleansed" or "filtered" before being written to the database, and this common task can be performed automatically by the framework using the information contained with the modified structure. This will ensure that: - Any field marked as "required" is not empty.
- All string fields contain strings which do not exceed their maximum size.
- All numeric fields contain numbers which do not exceed their maximum size, or fall below their minimum value.
- All boolean fields contain a value which is either TRUE or FALSE.
After this validation has been performed the data can be written to the database using the following code: function _cm_updateSelection($fieldarray, $replace) { $errors = array(); $this->fieldspec = $this->getFieldSpec_original();
$pkeynames = $this->getPkeyNames();
$rowdata = array(); $rownum = 0; foreach ($updatearray as $fieldname => $fieldvalue) { $rowdata[$rownum]['record_id'] = 'system'; $rowdata[$rownum]['field_id'] = $fieldname; $rowdata[$rownum]['field_value'] = $fieldvalue; $where = array2where($rowdata[$rownum], $pkeynames);
$count = $this->getCount($where); if ($count == 0) { $rowdata[$rownum] = $this->insertRecord($rowdata[$rownum]); } else { $rowdata[$rownum] = $this->updateRecord($rowdata[$rownum]); }
if (!empty($this->errors)) { $errors[$fieldname] = array_shift($this->errors); } $rownum = $rownum + 1; }
$this->errors = $errors;
return $fieldarray;
}
The getFieldSpec_original() method is used to replace the modified structure with the original structure. It then steps through the
input array and extracts each field which it then treats as a separate database
row. This row is then inserted or updated, as appropriate.
Tutorial Pages:
»
Introduction
»
A flexible approach
»
Implementation
»
From database to screen
» From screen to database
»
Summary
|

|