diff --git a/src/Db/Query/Query.php b/src/Db/Query/Query.php index 7cff2c27e..d84befe8d 100644 --- a/src/Db/Query/Query.php +++ b/src/Db/Query/Query.php @@ -1293,9 +1293,21 @@ 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)) { - return array_column($records, $rawFields[0], $key); + $column = $rawFields[0]; + if (strpos($column, '.')) + { + [, $column] = explode('.', $column); + } + + return array_column($records, $column, $key); } else { 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 */