/** * Create a new entry in the database based on current Model data * * Runs an INSERT query against the database, based on the current * contents of the "dirty" array. * * @return Model */ protected function _insert() { $table = $this->table(); $cols = $this->columns(); $names = array(); $holds = array(); $vals = array(); $rets = array(); $x = 1; foreach ($cols as $name => $col) { $qname = Database::quote_identifier($name); array_push($rets, $qname); if ($col->primary_key()) { continue; } $val = $col->prep_for_database($this->column($name)); array_push($names, $qname); array_push($holds, '$' . $x++); array_push($vals, $val); } $names = join(', ', $names); $holds = join(', ', $holds); $rets = join(', ', $rets); $table = Database::quote_identifier($table); $query = "INSERT INTO {$table} ({$names}) VALUES ({$holds}) " . "RETURNING {$rets}"; $results = Database::prefetch($query, $vals, "_insert_{$table}"); $this->_set_all($results[0]); return $this; }