Doctrine\DBAL\Connection::insert PHP Method

insert() public method

Table expression and columns are not escaped and are not safe for user-input.
public insert ( string $tableExpression, array $data, array $types = [] ) : integer
$tableExpression string The expression of the table to insert data into, quoted or unquoted.
$data array An associative array containing column-value pairs.
$types array Types of the inserted data.
return integer The number of affected rows.
    public function insert($tableExpression, array $data, array $types = array())
    {
        if (empty($data)) {
            return $this->executeUpdate('INSERT INTO ' . $tableExpression . ' ()' . ' VALUES ()');
        }
        return $this->executeUpdate('INSERT INTO ' . $tableExpression . ' (' . implode(', ', array_keys($data)) . ')' . ' VALUES (' . implode(', ', array_fill(0, count($data), '?')) . ')', array_values($data), is_string(key($types)) ? $this->extractTypeValues($data, $types) : $types);
    }

Usage Example

コード例 #1
2
 /**
  * @param string $tableName
  * @param array $rows
  */
 protected function insertTableRows($tableName, array $rows)
 {
     foreach ($rows as $rowKey => $values) {
         $this->connection->insert($tableName, $this->parser->parse($values));
         $this->parser->addReference($rowKey, $this->connection->lastInsertId());
     }
 }
All Usage Examples Of Doctrine\DBAL\Connection::insert