Automatically applies [[Solar_Sql_Adapter::quote() | ]] to the data
values for you.
For example:
{{code: php
$sql = Solar::factory('Solar_Sql');
$table = 'invaders';
$data = array(
'foo' => 'bar',
'baz' => 'dib',
'zim' => 'gir'
);
$rows_affected = $sql->insert($table, $data);
calls 'INSERT INTO invaders (foo, baz, zim) VALUES ("bar", "dib", "gir")'
}}
/** * * Inserts a new session-data row in the database. * * @param string $id The session ID. * * @param string $data The serialized session data. * * @return bool * */ protected function _insert($id, $data) { $now = date('Y-m-d H:i:s'); $cols = array($this->_config['created_col'] => $now, $this->_config['updated_col'] => $now, $this->_config['id_col'] => $id, $this->_config['data_col'] => $data); try { $this->_sql->insert($this->_config['table'], $cols); return true; } catch (Solar_Sql_Exception $e) { // @todo log this somehow? return false; } }
/** * * Inserts one row to the model table and deletes cache entries. * * @param array $data The row data to insert. * * @return int|bool On success, the last inserted ID if there is an * auto-increment column on the model (otherwise boolean true). On failure * an exception from PDO bubbles up. * * @throws Solar_Sql_Exception on failure of any sort. * * @see Solar_Sql_Model_Cache::deleteAll() * */ public function insert($data) { if (!is_array($data)) { throw $this->_exception('ERR_DATA_NOT_ARRAY', array('method' => 'insert')); } // reset affected rows $this->_affected_rows; // remove non-existent table columns from the data foreach ($data as $key => $val) { if (empty($this->_table_cols[$key])) { unset($data[$key]); // not in the table, so no need to check for autoinc continue; } // remove empty autoinc columns to soothe postgres, which won't // take explicit NULLs in SERIAL cols. if ($this->_table_cols[$key]['autoinc'] && empty($val)) { unset($data[$key]); } } // perform the insert and track affected rows $this->_affected_rows = $this->_sql->insert($this->_table_name, $data); // does the table have an autoincrement column? $autoinc = null; foreach ($this->_table_cols as $name => $info) { if ($info['autoinc']) { $autoinc = $name; break; } } // return the last insert id, or just "true" ? if ($autoinc) { $id = $this->_sql->lastInsertId($this->_table_name, $autoinc); } // clear the cache for this model and related models $this->_cache->deleteAll(); if ($autoinc) { return $id; } else { return true; } }