Skip to content
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
9 changes: 4 additions & 5 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3257,18 +3257,17 @@ protected function compileOrderBy(): string
{
if (is_array($this->QBOrderBy) && $this->QBOrderBy !== []) {
foreach ($this->QBOrderBy as &$orderBy) {
if (is_string($orderBy)) {
continue;
}
if ($orderBy['escape'] !== false && ! $this->isLiteral($orderBy['field'])) {
$orderBy['field'] = $this->db->protectIdentifiers($orderBy['field']);
}

$orderBy = $orderBy['field'] . $orderBy['direction'];
}

return $this->QBOrderBy = "\nORDER BY " . implode(', ', $this->QBOrderBy);
}

if (is_string($this->QBOrderBy)) {
return $this->QBOrderBy;
return "\nORDER BY " . implode(', ', $this->QBOrderBy);
}

return '';
Expand Down
20 changes: 20 additions & 0 deletions tests/system/Database/Builder/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,24 @@ public function testSelectResetQuery(): void
str_replace("\n", ' ', $sql),
);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/9696
*/
public function testGetCompiledSelect(): void
{
$builder = new BaseBuilder('users', $this->db);

$builder->select('name, role')->orderBy('name', 'desc');

$expected = 'SELECT "name", "role" FROM "users" ORDER BY "name" DESC';

$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect(false)));

$builder->orderBy('role', 'desc');

$expected = 'SELECT "name", "role" FROM "users" ORDER BY "name" DESC, "role" DESC';

$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.6.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Bugs Fixed

- **Database:** Fixed a bug in ``Database::connect()`` which was causing to store non-shared connection instances in shared cache.
- **Database:** Fixed a bug in ``Connection::getFieldData()`` for ``SQLSRV`` and ``OCI8`` where extra characters were returned in column default values (specific to those handlers), instead of following the convention used by other drivers.
- **Database:** Fixed a bug in ``BaseBuilder::compileOrderBy()`` where the method could overwrite ``QBOrderBy`` with a string instead of keeping it as an array, causing type errors and preventing additional ``ORDER BY`` clauses from being appended.
- **Forge:** Fixed a bug in ``Postgre`` and ``SQLSRV`` where changing a column's default value using ``Forge::modifyColumn()`` method produced incorrect SQL syntax.
- **Model:** Fixed a bug in ``Model::replace()`` where ``created_at`` field (when available) wasn't set correctly.

Expand Down
Loading