From 41b8c2e9a52245915e8a7074bf04814ee577f571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=AF=BAEno?= <895183594@qq.com> Date: Thu, 31 Oct 2024 16:32:45 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=AE=8C=E5=96=84Query=E7=9A=84column?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=BB=A5=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E8=A1=A8=E5=88=AB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Db/Query/Query.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Db/Query/Query.php b/src/Db/Query/Query.php index 7cff2c27e..4d8c1e92c 100644 --- a/src/Db/Query/Query.php +++ b/src/Db/Query/Query.php @@ -1295,7 +1295,13 @@ public function column($fields, ?string $key = null): array $records = $result->getStatementRecords(); if (1 === \count($rawFields)) { - return array_column($records, $rawFields[0], $key); + $column = $rawFields[0]; + if (strpos($column, '.')) + { + [, $column] = explode('.', $column); + } + + return array_column($records, $column, $key); } else { From 3a0f96ac89e1803b1c60a69dc627ebb7a3bde316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=AF=BAEno?= <895183594@qq.com> Date: Mon, 4 Nov 2024 11:15:24 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=B4=A2=E5=BC=95=E5=88=97=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E5=88=AB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Db/Query/Query.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Db/Query/Query.php b/src/Db/Query/Query.php index 4d8c1e92c..55c6ca60d 100644 --- a/src/Db/Query/Query.php +++ b/src/Db/Query/Query.php @@ -1293,6 +1293,12 @@ public function column($fields, ?string $key = null): array ->select(); $records = $result->getStatementRecords(); + + if (is_string($key) && strpos($key, '.')) + { + [, $key] = explode('.', $key); + } + if (1 === \count($rawFields)) { $column = $rawFields[0]; From 3438cf764a868f07f273600876a7a140aac65bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=AF=BAEno?= <895183594@qq.com> Date: Mon, 4 Nov 2024 11:17:40 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=9A=E9=80=9A=E8=BF=87=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E8=A1=A8=E5=88=AB=E5=90=8D=E8=8E=B7=E5=8F=96=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=88=97=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/Component/Tests/Db/DbBaseTest.php | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/unit/Component/Tests/Db/DbBaseTest.php b/tests/unit/Component/Tests/Db/DbBaseTest.php index 52f499765..450775550 100644 --- a/tests/unit/Component/Tests/Db/DbBaseTest.php +++ b/tests/unit/Component/Tests/Db/DbBaseTest.php @@ -674,6 +674,38 @@ public function testColumn(array $args): void $this->assertEquals(array_column_ex($origin, ['title', 'content', 'time', 'id'], 'id'), $data); } + /** + * @depends testBatchInsert + */ + public function testAliasColumn(array $args): void + { + $origin = $args['origin']; + + $data = Db::query($this->poolName) + ->table('tb_article', 't') + ->column('t.content'); + + $this->assertEquals(array_column($origin, 'content'), $data); + + $data = Db::query($this->poolName) + ->table('tb_article', 't') + ->column('t.content', 't.id'); + + $this->assertEquals(array_column($origin, 'content', 'id'), $data); + + $data = Db::query($this->poolName) + ->table('tb_article', 't') + ->column(['t.id', 't.content'], 't.id'); + + $this->assertEquals(array_column_ex($origin, ['id', 'content'], 'id'), $data); + + $data = Db::query($this->poolName) + ->table('tb_article', 't') + ->column(['t.title', 't.content', 't.time'], 't.id'); + + $this->assertEquals(array_column_ex($origin, ['title', 'content', 'time', 'id'], 'id'), $data); + } + /** * @depends testInsert */ From 9d5d6a64c2ee828db4b3592956da0d762e2bb479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=AF=BAEno?= <895183594@qq.com> Date: Mon, 4 Nov 2024 11:19:32 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Db/Query/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Db/Query/Query.php b/src/Db/Query/Query.php index 55c6ca60d..d84befe8d 100644 --- a/src/Db/Query/Query.php +++ b/src/Db/Query/Query.php @@ -1294,7 +1294,7 @@ public function column($fields, ?string $key = null): array $records = $result->getStatementRecords(); - if (is_string($key) && strpos($key, '.')) + if (\is_string($key) && strpos($key, '.')) { [, $key] = explode('.', $key); }