For example,
php
$sql = $queryBuilder->insert('user', [
'name' => 'Sam',
'age' => 30,
], $params);
The method will properly escape the table and column names.
public insert ( string $table, array $columns, array &$params ) : string | ||
$table | string | the table that new rows will be inserted into. |
$columns | array | the column data (name => value) to be inserted into the table. |
$params | array | the binding parameters that will be generated by this method. They should be bound to the DB command later. |
리턴 | string | the INSERT SQL |
/** * @inheritdoc */ public function insert($table, $columns, &$params) { $schema = $this->db->getSchema(); if (($tableSchema = $schema->getTableSchema($table)) !== null) { $columnSchemas = $tableSchema->columns; } else { $columnSchemas = []; } //Empty insert if (empty($columns) && !empty($columnSchemas)) { $columns = []; foreach ($columnSchemas as $columnSchema) { if (!$columnSchema->autoIncrement) { $columns[$columnSchema->name] = $columnSchema->defaultValue; } } } foreach ($columns as $name => $value) { if ($value instanceof Expression) { $columns[$name] = $this->convertExpression($value); } elseif (isset($columnSchemas[$name]) && in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) { $columns[$name] = [$value, \PDO::PARAM_LOB]; } } return parent::insert($table, $columns, $params); }
/** * @inheritdoc */ public function insert($table, $columns, &$params) { $schema = $this->db->getSchema(); if (($tableSchema = $schema->getTableSchema($table)) !== null) { $columnSchemas = $tableSchema->columns; } else { $columnSchemas = []; } foreach ($columns as $name => $value) { if (in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) { $columns[$name] = [$value, 'blob']; } } return parent::insert($table, $columns, $params); }
/** * Creates an INSERT SQL statement. * For example, * * ~~~ * $sql = $queryBuilder->insert('user', [ * 'name' => 'Sam', * 'age' => 30, * ], $params); * ~~~ * * The method will properly escape the table and column names. * * @param string $table the table that new rows will be inserted into. * @param array $columns the column data (name => value) to be inserted into the table. * @param array $params the binding parameters that will be generated by this method. * They should be bound to the DB command later. * @return string the INSERT SQL */ public function insert($table, $columns, &$params) { if (empty($columns)) { $schema = $this->db->getSchema(); if (($tableSchema = $schema->getTableSchema($table)) !== null && $tableSchema->sequenceName !== null) { $columns[$tableSchema->sequenceName] = 0; } } return parent::insert($table, $columns, $params); }
/** * @inheritdoc */ public function insert($table, $columns, &$params) { return parent::insert($table, $this->normalizeTableRowData($table, $columns), $params); }