ORM::save PHP Method

save() public method

Save any fields which have been modified on this object to the database.
public save ( )
    public function save()
    {
        $query = array();
        // remove any expression fields as they are already baked into the query
        $values = array_values(array_diff_key($this->_dirty_fields, $this->_expr_fields));
        if (!$this->_is_new) {
            // UPDATE
            // If there are no dirty values, do nothing
            if (empty($values) && empty($this->_expr_fields)) {
                return true;
            }
            $query = $this->_build_update();
            $id = $this->id(true);
            if (is_array($id)) {
                $values = array_merge($values, array_values($id));
            } else {
                $values[] = $id;
            }
        } else {
            // INSERT
            $query = $this->_build_insert();
        }
        $success = self::_execute($query, $values, $this->_connection_name);
        $caching_auto_clear_enabled = self::$_config[$this->_connection_name]['caching_auto_clear'];
        if ($caching_auto_clear_enabled) {
            self::clear_cache($this->_table_name, $this->_connection_name);
        }
        // If we've just inserted a new record, set the ID of this object
        if ($this->_is_new) {
            $this->_is_new = false;
            if ($this->count_null_id_columns() != 0) {
                $db = self::get_db($this->_connection_name);
                if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') {
                    // it may return several columns if a compound primary
                    // key is used
                    $row = self::get_last_statement()->fetch(PDO::FETCH_ASSOC);
                    foreach ($row as $key => $value) {
                        $this->_data[$key] = $value;
                    }
                } else {
                    $column = $this->_get_id_column_name();
                    // if the primary key is compound, assign the last inserted id
                    // to the first column
                    if (is_array($column)) {
                        $column = reset($column);
                    }
                    $this->_data[$column] = $db->lastInsertId();
                }
            }
        }
        $this->_dirty_fields = $this->_expr_fields = array();
        return $success;
    }

Usage Example

Example #1
0
 /**
  * Create operation
  *
  * @param  array $data
  * @return ORM
  */
 public function create(array $data = array())
 {
     foreach ($data as $column => $value) {
         $this->orm->set($column, $value);
     }
     return $this->orm->save();
 }
All Usage Examples Of ORM::save
ORM