" * @license GPLv2 * @package generis * */ trait common_persistence_sql_MultipleOperations { /** @var common_persistence_sql_UpdateMultiple */ private $updateMultiple = null; public function insertMultiple($tableName, array $data, array $types = []) { if (is_array($data) && count($data) > 0) { $platform = $this->getPlatform(); $quotedColumnIdentifiers = array_map( function ($value) use ($platform) { return $platform->quoteIdentifier($value); }, array_keys($data[0]) ); $query = "INSERT INTO ${tableName} (" . implode(', ', $quotedColumnIdentifiers) . ') VALUES '; $valuesQueries = []; $allValues = []; foreach ($data as $values) { $valuesQueries[] .= '(' . implode(', ', array_fill(0, count($values), '?')) . ')'; $allValues = array_merge($allValues, array_values($values)); } $query .= implode(', ', $valuesQueries); return $this->exec($query, $allValues, $types); } else { return 0; } } /** * @example * 'table_name' * 'data' => * [ * [ * 'conditions' => [ * 'c1' => 'c1value', * 'c2' => 'c2value' * ] * 'updateValues' => [ * 'c3' => 'c3value' * ] * ], * [ * 'conditions' => [ * 'c1' => 'c1value', * 'c2' => 'c2value', * 'c3' => 'c3value', * ] * 'updateValues' => [ * 'c9' => 'c8value' * ] * ] * ] * * @param string $table * @param array $data * @return bool * @throws Exception */ public function updateMultiple($table, array $data) { if ($this->updateMultiple === null) { $this->updateMultiple = new common_persistence_sql_UpdateMultiple($this->getDbalConnection()); } return $this->updateMultiple->updateMultiple($table, $data); } }