/** * Saves the current record. * * Examples: * ```php * // saves a single column * $r->save("col0", "val0"); * * // saves multiple columns * $r->save(["col0" => "val0", "col1" => "val1", "col2" => "val2"]); * ``` * * @param array $colVals Column values * * @return void */ public function save($colVals = []) { $ret = []; // method overloading if (func_num_args() > 1) { $colPath = func_get_arg(0); $colVal = func_get_arg(1); $colVals = [$colPath => $colVal]; } if (count($colVals) > 0) { // registers columns and saves $cols = $this->_regColumns(array_keys($colVals)); $vals = array_values($colVals); foreach ($cols as $i => $col) { $col->setValue($vals[$i]); } $this->save(); } else { // first saves the 'left join' tables foreach ($this->_tables as $table) { $table->save(); } $columns = $this->_getChangedColumns(); if ($this->_primaryKey->hasChanged()) { // update if (count($columns) > 0) { $this->_db->exec($this->_getUpdateStatement($columns)); } } else { // insert $this->_db->exec($this->_getInsertStatement($columns)); $row = $this->_db->query("select last_insert_id() as id"); $this->_primaryKey->setValue($row["id"]); } // resets columns foreach ($columns as $column) { $column->reset(); } $this->_isUpdated = false; } return $ret; }
/** * Inserts or updates the current record. * * @return void */ public function save() { // first saves the 'left join' tables foreach ($this->_leftJoins as $leftJoin) { $leftJoin->save(); } $columns = $this->_getChangedColumns(); if ($this->_primaryKey->hasChanged()) { // update if (count($columns) > 0) { $this->_db->exec($this->_getUpdateStatement($columns)); } } else { // insert $this->_db->exec($this->_getInsertStatement($columns)); $row = $this->_db->query("select last_insert_id() as id"); $this->_primaryKey->setValue($row["id"]); } // resets columns foreach ($columns as $column) { $column->reset(); } $this->_isUpdated = false; }