Fields=$fields; } /** * Sets current item field value * (applies formatting) * * @access public * @param string $name Name of the field * @param mixed $value Value to set the field to * @return void */ function SetField($name,$value) { $this->SetDBField($name,$value); } /** * Sets current item field value * (doesn't apply formatting) * * @access public * @param string $name Name of the field * @param mixed $value Value to set the field to * @return void */ function SetDBField($name,$value) { $this->FieldValues[$name] = $value; } /** * Return current item' field value by field name * (apply formatter) * * @access public * @param string $name field name to return * @return mixed */ function GetField($name) { return $this->GetDBField($name); } /** * Return current item' field value by field name * (doesn't apply formatter) * * @access public * @param string $name field name to return * @return mixed */ function GetDBField($name) { return $this->FieldValues[$name]; } /** * Sets item' fields corresponding to elements in passed $hash values. * * The function sets current item fields to values passed in $hash, by matching $hash keys with field names * of current item. If current item' fields are unknown {@link kDBItem::PrepareFields()} is called before acutally setting the fields * * @access public * @param Array $hash * @return void */ function SetFieldsFromHash($hash) { foreach ($hash as $field_name => $field_value) { if( eregi("^[0-9]+$", $field_name) || !in_array($field_name,$this->Fields) ) continue; $this->SetField($field_name,$field_value); } } /** * Returns part of SQL WHERE clause identifing the record, ex. id = 25 * * @access public * @param string $method Child class may want to know who called GetKeyClause, Load(), Update(), Delete() send its names as method * @return void * @see kDBItem::Load() * @see kDBItem::Update() * @see kDBItem::Delete() */ function GetKeyClause($method=null) { return $this->IDField.' = '.$this->Conn->qstr($this->ID); } /** * Loads item from the database by given id * * @access public * @param int $id Primery Key Id to load * @param string $id_field_name Optional parameter to load item by given Id field * @return bool True if item has been loaded, false otherwise */ function Load($id, $id_field_name=null) { if (isset($id_field_name)) $this->SetIDField($id_field_name); if (!isset($id)) return false; $this->ID = $id; $q = $this->GetSelectSQL().' WHERE '.$this->GetKeyClause('load'); if ($this->DisplayQueries) { echo get_class($this)." Load SQL: $q
"; } $this->FieldValues = $this->Conn->GetRow($q); if ($this->FieldValues === false) { //Error handling could be here return false; } $this->setID($id); return true; } /** * Updates previously loaded record with current item' values * * @access public * @param int Primery Key Id to update * @return void */ function Update($id=null) { if( isset($id) ) $this->ID=$id; if( !isset($this->ID) ) return false; // Validate before updating if( !$this->Validate() ) return false; //Nothing to update if(!$this->FieldValues) return true; $sql = sprintf('UPDATE %s SET ',$this->TableName); foreach ($this->FieldValues as $field_name => $field_value) { if ( isset($this->VirtualFields[$field_name]) ) continue; //skipping 'virtual' field if ($field_name == $this->IDField) continue; //skipping Primary Key $real_field_name = eregi_replace("^.*\.", '',$field_name); //removing table names from field names //Adding part of SET clause for current field, escaping data with ADODB' qstr $sql.= sprintf('%s=%s, ',$real_field_name,$this->Conn->qstr($this->FieldValues[$field_name], 0)); } $sql = ereg_replace(", $", '', $sql); //Removing last comma and space $sql.= sprintf(' WHERE %s', $this->GetKeyClause('update')); //Adding WHERE clause with Primary Key if ($this->DisplayQueries) echo "Sql: $sql
"; if ($this->Conn->ChangeQuery($sql) === false) { //Executing query and checking results if ($this->DisplayQueries) { echo "Error executing statement: ".$adodbConnection->ErrorMsg()."
"; } return false; } return true; } function Validate() { return true; } /** * Creates a record in the database table with current item' values * * @access public * @return void */ function Create() { if(!$this->Validate()) //Validating fields before attempting to create record return false; $fields_sql = ''; $values_sql = ''; foreach ($this->FieldValues as $field_name => $field_value) { if ( isset($this->VirtualFields[$field_name]) ) continue; //skipping 'virtual' field $fields_sql .= sprintf('%s, ',$field_name); //Adding field name to fields block of Insert statement //Adding field' value to Values block of Insert statement, escaping it with ADODB' qstr $values_sql .= sprintf('%s, ',$this->Conn->qstr($this->FieldValues[$field_name], 0)); } //Cutting last commas and spaces $fields_sql = ereg_replace(", $", '', $fields_sql); $values_sql = ereg_replace(", $", '', $values_sql); $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->TableName, $fields_sql, $values_sql); //Formatting query //Executing the query and checking the result if ($this->Conn->ChangeQuery($sql) === false) { if($this->DisplayQueries) { echo "Error executing statement: ".$this->Conn->getErrorMsg().'
'; } return false; } $this->setID( $this->Conn->getInsertID() ); //$this->SetInsertID(); //Setting Primary Key ($this->id) for futher using the object return true; } /** * Deletes the record from databse * * @access public * @return void */ function Delete() { $q = 'DELETE FROM '.$this->TableName.' WHERE '.$this->GetKeyClause('Delete'); if ($this->DisplayQueries) { echo get_class($this).' Delete SQL: '.$q.'
'; } return $this->Conn->ChangeQuery($q); } /** * Set's new ID for item * * @param int $new_id * @access public */ function setID($new_id) { $this->ID=$new_id; $this->SetDBField($this->IDField,$new_id); } } ?>