You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit e2b6225
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: tests/system/Database/Builder/JoinTest.php
+50Lines changed: 50 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -121,6 +121,44 @@ public function testJoinRawSql(): void
121
121
$this->assertSame($expectedSQL, $output);
122
122
}
123
123
124
+
publicfunctiontestJoinRawSqlTable(): void
125
+
{
126
+
$builder = newBaseBuilder('users', $this->db);
127
+
128
+
$builder->join(
129
+
newRawSql('(SELECT user_id, MAX(created_at) AS latest FROM posts GROUP BY user_id) recent'),
130
+
'recent.user_id = users.id',
131
+
'LEFT',
132
+
);
133
+
134
+
$expectedSQL = 'SELECT * FROM "users" LEFT JOIN (SELECT user_id, MAX(created_at) AS latest FROM posts GROUP BY user_id) recent ON "recent"."user_id" = "users"."id"';
Copy file name to clipboardExpand all lines: user_guide_src/source/changelogs/v4.8.0.rst
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,6 +74,7 @@ update your implementations to include the new methods or method changes to ensu
74
74
Method Signature Changes
75
75
========================
76
76
77
+
- **Database:** ``BaseBuilder::join()`` and ``BaseBuilder::compileJoinTable()`` now accept ``RawSql|string`` for the table argument. Custom builders overriding either method must accept both types. See :doc:`../installation/upgrade_480`.
77
78
- **CLI:** The ``Console::run()`` method now accepts an optional ``array $tokens`` parameter. This allows you to pass an array of command tokens directly to the console runner, which is useful for testing or programmatically running commands. If not provided, it will default to using the global ``$argv``.
78
79
- **CodeIgniter:** The deprecated parameters in methods have been removed:
79
80
- ``CodeIgniter\CodeIgniter::handleRequest()`` no longer accepts the deprecated ``$cacheConfig`` and ``$returnResponse`` parameters.
@@ -281,6 +282,7 @@ Database
281
282
Query Builder
282
283
-------------
283
284
285
+
- ``BaseBuilder::join()`` now accepts ``RawSql`` as the table argument, allowing raw JOIN targets such as subqueries. See :ref:`query-builder-join-rawsql`.
284
286
- Added ``exists()`` and ``doesntExist()`` to Query Builder to check whether the current Query Builder query would return at least one row. See :ref:`query-builder-exists`.
285
287
- Added ``explain()`` to Query Builder to run execution-plan queries for the current ``SELECT`` query. See :ref:`query-builder-explain`.
286
288
- Added ``havingBetween()``, ``orHavingBetween()``, ``havingNotBetween()``, and ``orHavingNotBetween()`` to Query Builder. See :ref:`query-builder-having-between`.
Copy file name to clipboardExpand all lines: user_guide_src/source/database/query_builder.rst
+16-2Lines changed: 16 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -301,6 +301,19 @@ Since v4.2.0, ``$builder->join()`` accepts a ``CodeIgniter\Database\RawSql`` ins
301
301
302
302
.. literalinclude:: query_builder/102.php
303
303
304
+
Since v4.8.0, the table can also be a ``RawSql`` instance. For example, you
305
+
can join a subquery:
306
+
307
+
.. code-block:: php
308
+
309
+
use CodeIgniter\Database\RawSql;
310
+
311
+
$builder->join(
312
+
new RawSql('(SELECT user_id, MAX(created_at) AS latest FROM posts GROUP BY user_id) recent'),
313
+
'recent.user_id = users.id',
314
+
'LEFT'
315
+
);
316
+
304
317
.. warning:: When you use ``RawSql``, you MUST escape the values and protect the identifiers manually. Failure to do so could result in SQL injections.
0 commit comments