/**
  * Save the object to the database.
  * @return object
  */
 public function save()
 {
     if (count($this->modifiedColumns) > 0) {
         $sql = array();
         foreach ($this->modifiedColumns as $field => $j) {
             if ($field == $this->getIdField() && !$this->isAutoIncremented()) {
                 if ($this->getId() == null) {
                     throw new ErrorException($this->getIdField() . " is required for tables without an autoincrement id.");
                 }
                 $sql[] = $this->getTableName() . "." . $field . ' = ' . $this->factory->escapeString($this->getId()) . '';
             } elseif ($field != $this->getIdField() && in_array($field, static::getFields())) {
                 if ($this[$field] !== null) {
                     $sql[] = $this->getTableName() . "." . $field . ' = ' . $this->factory->escapeString($this[$field]) . '';
                 } else {
                     $sql[] = $this->getTableName() . "." . $field . ' = NULL';
                 }
             }
         }
         if ($this->isNewObject()) {
             $this->factory->update('INSERT INTO ' . $this->getTableName() . " SET " . implode(",", $sql));
             if ($this->isAutoIncremented()) {
                 $this[$this->getIdField()] = $this->factory->getLastInsertId();
             }
         } else {
             $this->factory->update('UPDATE ' . $this->getTableName() . " SET " . implode(",", $sql) . " WHERE " . $this->getTableName() . "." . $this->getIdField() . ' = ' . $this->factory->escapeString($this->getId()));
         }
     }
     $this->clearModifiedColumns();
     $this->isNewObject = false;
     return $this;
 }