Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完善Query的column方法以支持指定表别名 #712

Open
wants to merge 4 commits into
base: 2.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Db/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/Component/Tests/Db/DbBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading