/** Set the query field to a SQL string that saves the specified object field values in the database.
* @param anObject Object whose field values need to be saved
* @tableName String
* @param fieldMap Associative Array mapping fieldName to columnName
* @param insert wheather a record for the object should be inserted. If false the objects record will be updated
*/
function ($anObject, $tableName, $fieldMap, $insert) {
if ($insert) return $this->($anObject, $tableName, $fieldMap);
$sql = "update $tableName SET ";
$sep = "";
reset($fieldMap);
forEach($fieldMap as $field => $column) {
//insert of a not-new object is assumed to be insert in secondary table
if ($field != 'id' ) {
$sql .= $sep;
$sql .= $column;
$sql .= '=';
$sql .= $this->(isSet($anObject->$field) ? $anObject->$field : null);
$sep = ', ';
}
}
$this->query = $sql;
$this->(
$fieldMap['id'] // the column name for field 'id'
, $anObject->id
);
}
|