/**
  * Saves the object back to the database
  * eg:
  * <code>
  * $car = MyActiveRecord::Create('Car');
  * print $car->id;  // NULL
  * $car->save();
  * print $car->id; // 1
  * </code>
  *
  * NB: if the object has registered errors, save() will return false
  * without attempting to save the object to the database
  *
  * @return  boolean true on success false on fail
  */
 public function save()
 {
     // if this object has registered errors, we back off and return false.
     if ($this->get_errors()) {
         return false;
     } else {
         $table = MyActiveRecord::Class2Table(get_class($this));
         // check for single-table-inheritance
         if (strtolower(get_class($this)) != $table) {
             $this->class = get_class($this);
         }
         $fields = MyActiveRecord::Columns($table);
         // sort out key and value pairs
         foreach ($fields as $key => $field) {
             if ($key != 'id') {
                 $val = MyActiveRecord::Escape(isset($this->{$key}) ? $this->{$key} : null);
                 $vals[] = $val;
                 $keys[] = "`" . $key . "`";
                 $set[] = "`{$key}` = {$val}";
             }
         }
         // insert or update as required
         if (isset($this->id)) {
             $sql = "UPDATE `{$table}` SET " . implode($set, ", ") . " WHERE `id`={$this->id}";
         } else {
             $sql = "INSERT INTO `{$table}` (" . implode($keys, ", ") . ") VALUES (" . implode($vals, ", ") . ")";
         }
         $success = MyActiveRecord::Query($sql);
         if (!isset($this->id)) {
             $this->id = mysql_insert_id(MyActiveRecord::Connection());
         }
         return $success;
     }
 }