From 0b28ffbe8cfc2ab0dc12ca3cf86e8fc76616d0d3 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Thu, 21 Dec 2023 14:43:59 +0700 Subject: [PATCH] Fix `Command::insertWithReturningPks()` (#250) Co-authored-by: Alexey Rogachev --- CHANGELOG.md | 1 + src/Command.php | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6151080..ffe011f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 1.2.1 under development - Enh #248: Change property `Schema::$typeMap` to constant `Schema::TYPE_MAP` (@Tigrov) +- Bug #250: Fix `Command::insertWithReturningPks()` method for table without primary keys (@Tigrov) ## 1.2.0 November 12, 2023 diff --git a/src/Command.php b/src/Command.php index 549f2c1..3c85f7f 100644 --- a/src/Command.php +++ b/src/Command.php @@ -22,14 +22,21 @@ final class Command extends AbstractPdoCommand { public function insertWithReturningPks(string $table, array $columns): bool|array { + $tableSchema = $this->db->getSchema()->getTableSchema($table); + $returnColumns = $tableSchema?->getPrimaryKey() ?? []; + + if ($returnColumns === []) { + if ($this->insert($table, $columns)->execute() === 0) { + return false; + } + + return []; + } + $params = []; $sql = $this->getQueryBuilder()->insert($table, $columns, $params); - $tableSchema = $this->db->getSchema()->getTableSchema($table); - - $returnColumns = $tableSchema?->getPrimaryKey() ?? []; $columnSchemas = $tableSchema?->getColumns() ?? []; - $returnParams = []; $returning = [];