Solar_Sql_Adapter::insert PHP Method

insert() public method

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")' }}
public insert ( string $table, array $data ) : integer
$table string The table to insert data into.
$data array An associative array where the key is the column name and the value is the value to insert for that column.
return integer The number of rows affected, typically 1.
    public function insert($table, $data)
    {
        // the base statement
        $table = $this->quoteName($table);
        $stmt = "INSERT INTO {$table} ";
        // col names come from the array keys
        $keys = array_keys($data);
        // quote the col names
        $cols = array();
        foreach ($keys as $key) {
            $cols[] = $this->quoteName($key);
        }
        // add quoted col names
        $stmt .= '(' . implode(', ', $cols) . ') ';
        // add value placeholders (use unquoted key names)
        $stmt .= 'VALUES (:' . implode(', :', $keys) . ')';
        // execute the statement
        $result = $this->query($stmt, $data);
        return $result->rowCount();
    }

Usage Example

コード例 #1
0
ファイル: Sql.php プロジェクト: kalkin/solarphp
 /**
  * 
  * 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;
     }
 }
All Usage Examples Of Solar_Sql_Adapter::insert