Skip to content

Commit e6491a3

Browse files
authored
fix: compileOrderBy method (#9697)
1 parent ea9612f commit e6491a3

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

system/Database/BaseBuilder.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,18 +3257,17 @@ protected function compileOrderBy(): string
32573257
{
32583258
if (is_array($this->QBOrderBy) && $this->QBOrderBy !== []) {
32593259
foreach ($this->QBOrderBy as &$orderBy) {
3260+
if (is_string($orderBy)) {
3261+
continue;
3262+
}
32603263
if ($orderBy['escape'] !== false && ! $this->isLiteral($orderBy['field'])) {
32613264
$orderBy['field'] = $this->db->protectIdentifiers($orderBy['field']);
32623265
}
32633266

32643267
$orderBy = $orderBy['field'] . $orderBy['direction'];
32653268
}
32663269

3267-
return $this->QBOrderBy = "\nORDER BY " . implode(', ', $this->QBOrderBy);
3268-
}
3269-
3270-
if (is_string($this->QBOrderBy)) {
3271-
return $this->QBOrderBy;
3270+
return "\nORDER BY " . implode(', ', $this->QBOrderBy);
32723271
}
32733272

32743273
return '';

tests/system/Database/Builder/SelectTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,24 @@ public function testSelectResetQuery(): void
407407
str_replace("\n", ' ', $sql),
408408
);
409409
}
410+
411+
/**
412+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/9696
413+
*/
414+
public function testGetCompiledSelect(): void
415+
{
416+
$builder = new BaseBuilder('users', $this->db);
417+
418+
$builder->select('name, role')->orderBy('name', 'desc');
419+
420+
$expected = 'SELECT "name", "role" FROM "users" ORDER BY "name" DESC';
421+
422+
$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect(false)));
423+
424+
$builder->orderBy('role', 'desc');
425+
426+
$expected = 'SELECT "name", "role" FROM "users" ORDER BY "name" DESC, "role" DESC';
427+
428+
$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
429+
}
410430
}

user_guide_src/source/changelogs/v4.6.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Bugs Fixed
3232

3333
- **Database:** Fixed a bug in ``Database::connect()`` which was causing to store non-shared connection instances in shared cache.
3434
- **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.
35+
- **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.
3536
- **Forge:** Fixed a bug in ``Postgre`` and ``SQLSRV`` where changing a column's default value using ``Forge::modifyColumn()`` method produced incorrect SQL syntax.
3637
- **Model:** Fixed a bug in ``Model::replace()`` where ``created_at`` field (when available) wasn't set correctly.
3738
- **Model:** Fixed a bug in ``Model::insertBatch()`` and ``Model::updateBatch()`` where casts were not applied to inserted or updated values.

0 commit comments

Comments
 (0)