code: public function save()
      {
          $app = App::getInstance();
  
          // update model
          if ($this->getPk()->getValue() && !$this->new) {
              // Determine what has been modified
              $changed = array();
              foreach ($this->initialFields as $column => $field) {
                  if ($this->fields[$column]->getRawValue() !== $field->getRawValue()) {
                      $changed[$column] = $this->fields[$column];
                  }
              }
  
              if (!empty($changed)) {
                  $sql = $this->constructUpdateSQL($changed, $this->fields);
  
                  $this->runQuery($sql);
                  if ($app['db']->last_error) {
                      throw new SQLException(
                          $app['db']->last_error,
                          $app['db']->captured_errors
                      );
                  }
  
                  $this->constructorId = $this->getPk();
  
                  // clear cache
                  $item = $this->getCache($this->getPk());
                  $item->clear();
  
                  $this->log_update();
              }
          } else { // insert model
              $sql = $this->constructInsertSQL($this->fields);
  
              $this->runQuery($sql);
              if ($app['db']->last_error) {
                  throw new SQLException(
                      $app['db']->last_error,
                      $app['db']->captured_errors
                  );
              }
  
              $this->pk = $this->findPk($this->fields);
  
              if ($app['db']->insert_id) {
                  $this->fields[$this->pk]->setValue($app['db']->insert_id);
              }
  
              $this->new = false;
              $this->constructorId = $this->getPk();
  
              $this->log_create();
          }
  
          $this->initialFields = $this->fields;
  
          return $this->getPk()->getValue(); // return new id
      }
docs: Public: Save all fields to database
  
  Example:
  $obj = new Obj();
  $obj->setTable('comment');
  $obj->setUser('k.onions');
  $obj->setContent('hello');
  $obj->save();