Skip to content

Commit e0d4746

Browse files
authored
Bugfix for insertEx (#414)
1 parent d3f1ddf commit e0d4746

3 files changed

Lines changed: 28 additions & 28 deletions

File tree

src/Command/Command.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -409,33 +409,6 @@ public function insert(string $table, QueryInterface|array $columns): static
409409
return $this->setSql($sql)->bindValues($params);
410410
}
411411

412-
public function insertEx(string $table, array $columns): bool|array
413-
{
414-
$params = [];
415-
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
416-
$this->setSql($sql)->bindValues($params);
417-
418-
if (!$this->execute()) {
419-
return false;
420-
}
421-
422-
$tableSchema = $this->db->getSchema()->getTableSchema($table);
423-
$tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
424-
425-
$result = [];
426-
foreach ($tablePrimaryKeys as $name) {
427-
if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
428-
$result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
429-
continue;
430-
}
431-
432-
/** @psalm-var mixed */
433-
$result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
434-
}
435-
436-
return $result;
437-
}
438-
439412
public function noCache(): static
440413
{
441414
$this->queryCacheDuration = -1;

src/Driver/PDO/CommandPDO.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,33 @@ public function bindValues(array $values): static
101101
return $this;
102102
}
103103

104+
public function insertEx(string $table, array $columns): bool|array
105+
{
106+
$params = [];
107+
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
108+
$this->setSql($sql)->bindValues($params);
109+
110+
if (!$this->execute()) {
111+
return false;
112+
}
113+
114+
$tableSchema = $this->db->getSchema()->getTableSchema($table);
115+
$tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
116+
117+
$result = [];
118+
foreach ($tablePrimaryKeys as $name) {
119+
if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
120+
$result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
121+
continue;
122+
}
123+
124+
/** @psalm-var mixed */
125+
$result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
126+
}
127+
128+
return $result;
129+
}
130+
104131
/**
105132
* @throws Exception|InvalidConfigException|PDOException
106133
*/

tests/QueryBuilder/QueryBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testInsertEx(
112112

113113
$this->expectException(NotSupportedException::class);
114114
$this->expectExceptionMessage(
115-
'Yiisoft\Db\Tests\Support\Stub\Schema::loadTableSchema is not supported by this DBMS.'
115+
'Yiisoft\Db\QueryBuilder\DMLQueryBuilder::insertEx() is not supported by this DBMS.'
116116
);
117117

118118
$qb->insertEx($table, $columns, $params);

0 commit comments

Comments
 (0)