Skip to content

Commit 96b7f13

Browse files
committed
Fix: The subquery adds a prefix for the table alias.
1 parent c7e9e5d commit 96b7f13

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

system/Database/BaseBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2952,7 +2952,7 @@ protected function buildSubquery($builder, bool $wrapped = false, string $alias
29522952
throw new DatabaseException('The subquery cannot be the same object as the main query object.');
29532953
}
29542954

2955-
$subquery = strtr($builder->getCompiledSelect(), "\n", ' ');
2955+
$subquery = strtr($builder->getCompiledSelect(false), "\n", ' ');
29562956

29572957
if ($wrapped) {
29582958
$subquery = '(' . $subquery . ')';

tests/system/Database/Builder/PrefixTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,24 @@ public function testPrefixesSetOnTableNamesWithWhereClause()
4949
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
5050
$this->assertSame($expectedBinds, $builder->getBinds());
5151
}
52+
53+
public function testPrefixWithSubquery()
54+
{
55+
$expected = <<<'NOWDOC'
56+
SELECT "u"."id", "u"."name", (SELECT 1 FROM "ci_users" "sub" WHERE "sub"."id" = "u"."id") "one"
57+
FROM "ci_users" "u"
58+
WHERE "u"."id" = 1
59+
NOWDOC;
60+
61+
$subquery = $this->db->table('users sub')
62+
->select('1', false)
63+
->where('sub.id = u.id');
64+
65+
$builder = $this->db->table('users u')
66+
->select('u.id, u.name')
67+
->selectSubquery($subquery, 'one')
68+
->where('u.id', 1);
69+
70+
$this->assertSame($expected, $builder->getCompiledSelect());
71+
}
5272
}

user_guide_src/source/changelogs/v4.2.5.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ none.
3232

3333
Bugs Fixed
3434
**********
35+
- When using subqueries in the main query, prefixes are added to the table alias.
3536

3637
See the repo's `CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_ for a complete list of bugs fixed.

0 commit comments

Comments
 (0)