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

add: Added support of QueryBuilder column alias #209

Merged
merged 2 commits into from
Oct 9, 2018
Merged
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
4 changes: 2 additions & 2 deletions src/db/src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ protected function addOneDecorator()
throw new DbException('The result is not instanceof ' . $this->className);
}

if (isset($result[0]) && empty($this->join)) {
if (isset($result[0]) && empty($this->join) && !empty($this->className)) {
$tableName = $this->getTableName();

return EntityHelper::formatRowByType($result[0], $tableName);
Expand Down Expand Up @@ -1476,7 +1476,7 @@ protected function addGetDecorator()
return new Collection($entities);
}

if (!empty($result) && empty($this->join)) {
if (!empty($result) && empty($this->join) && !empty($this->className)) {
$tableName = $this->getTableName();
$result = EntityHelper::formatListByType($result, $tableName);
}
Expand Down
70 changes: 70 additions & 0 deletions src/db/test/Cases/Mysql/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,74 @@ public function testModelQuerySelectOne(int $id)
$result = User::query()->where('id', $id)->one()->getResult();
$this->assertEquals($id, $result->getId());
}

/**
* 列别名查询
* @throws \Swoft\Db\Exception\MysqlException
*/
public function testColumnAlias()
{
// 进行数据插入
$userData = [
'name' => 'name:'.uniqid(),
'sex' => 2,
'description' => 'this my desc',
'age' => 99,
];
$userId = Query::table(User::class)->insert($userData)->getResult();
// 查询条件
$where = ['id' => $userId];

// 进行查询,查询单个
$user = Query::table(User::class)->condition($where)
->one([
'sex' => 'sex',
'name' => 'nickName'
])->getResult();
// 比较结果, 判断是否为数组
$this->assertEquals(is_array($user), true);
// 判断是否有非属性值的key
$this->assertEquals($user['nickName'], $userData['name']);

// 通过Entity进行查询
$user = User::findOne($where)->getResult();
// 比较结果, 判断是否为User对象
$this->assertEquals($user instanceof User, true);
$this->assertEquals($user->getName(), $userData['name']);

// 进行查询,查询多个
$userList = Query::table(User::class)->condition($where)
->get([
'sex' => 'sex',
'name' => 'nickName'
])->getResult();
// array_pop
$user = array_pop($userList);
// 比较结果, 判断是否为数组
$this->assertEquals(is_array($user), true);
// 判断是否有非属性值的key
$this->assertEquals($user['nickName'], $userData['name']);

// 通过Entity获取列表
/**
* @var \Swoft\Db\Collection $userList
*/
$userList = User::findAll($where)->getResult()->all();
// array_pop
$user = array_pop($userList);
// 比较结果, 判断是否为User对象
$this->assertEquals($user instanceof User, true);
$this->assertEquals($user->getName(), $userData['name']);
}

/**
* 列别名查询
* @throws \Swoft\Db\Exception\MysqlException
*/
public function testColumnAliasByCo()
{
go(function () {
$this->testColumnAlias();
});
}
}