Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6554fca

Browse files
authored
Simplify insertEx (#155)
1 parent 21ec874 commit 6554fca

File tree

2 files changed

+6
-31
lines changed

2 files changed

+6
-31
lines changed

src/CommandPDO.php

-30
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,6 @@
2222

2323
final class CommandPDO extends AbstractCommandPDO
2424
{
25-
/**
26-
* @inheritDoc
27-
*/
28-
public function insertEx(string $table, array $columns): bool|array
29-
{
30-
$params = [];
31-
$sql = $this->db->getQueryBuilder()->insertEx($table, $columns, $params);
32-
$this->setSql($sql)->bindValues($params);
33-
34-
if (!$this->execute()) {
35-
return false;
36-
}
37-
38-
$tableSchema = $this->db->getSchema()->getTableSchema($table);
39-
$tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
40-
41-
$result = [];
42-
foreach ($tablePrimaryKeys as $name) {
43-
if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
44-
$result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
45-
continue;
46-
}
47-
48-
/** @psalm-var mixed */
49-
$result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
50-
}
51-
52-
return $result;
53-
}
54-
5525
public function queryBuilder(): QueryBuilderInterface
5626
{
5727
return $this->db->getQueryBuilder();

tests/QueryBuilderTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,12 @@ public function testInsertEx(
569569
string $expectedSQL,
570570
array $expectedParams
571571
): void {
572-
parent::testInsertEx($table, $columns, $params, $expectedSQL, $expectedParams);
572+
$this->expectException(NotSupportedException::class);
573+
$this->expectExceptionMessage('Yiisoft\Db\QueryBuilder\DMLQueryBuilder::insertEx() is not supported by this DBMS.');
574+
575+
$db = $this->getConnection(true);
576+
$qb = $db->getQueryBuilder();
577+
$qb->insertEx($table, $columns, $params);
573578
}
574579

575580
/**

0 commit comments

Comments
 (0)