@@ -271,17 +271,25 @@ public function orderBy($sort, $dir = 'desc') { return $this->sortBy($sort, $dir
271
271
* Join a table to this query
272
272
*
273
273
* @param string $table Name of table to select from
274
- * @param mixed[] $columnclauses List of on clauses in the format column = column
275
- * @param mixed[] $valueclauses List of on clauses in the format column = value
276
274
* @return self
277
275
*/
278
- public function join ($ table, $ columnclauses = [], $ valueclauses = [] )
276
+ public function join ($ table )
279
277
{
280
278
$ this ->joins [] = ['table ' => $ table , 'where ' => [], 'on ' => []];
281
279
282
- if ($ columnclauses ) $ this ->joinOn ($ columnclauses );
283
- if ($ valueclauses ) $ this ->joinWhere ($ valueclauses );
284
-
280
+ return $ this ;
281
+ }
282
+
283
+ /**
284
+ * Join a subquery as a derived table to this query
285
+ *
286
+ * @param mixed $subquery String or QueryBuilder object representing subquery
287
+ * @param string $alias Alias for the derived table
288
+ * @return self
289
+ */
290
+ public function joinSubquery ($ subquery , $ alias )
291
+ {
292
+ $ this ->joins [] = ['table ' => null , 'subquery ' => $ subquery , 'alias ' => $ alias , 'where ' => [], 'on ' => []];
285
293
return $ this ;
286
294
}
287
295
@@ -364,7 +372,7 @@ public function resolve()
364
372
$ sort = $ this ->resolveSort ();
365
373
$ limit = $ this ->resolveLimit ();
366
374
367
- return ["SELECT $ columns FROM $ table {$ join }{$ where }{$ group }{$ having }{$ sort }{$ limit }; " , $ this ->data ];
375
+ return ["SELECT $ columns FROM $ table {$ join }{$ where }{$ group }{$ having }{$ sort }{$ limit }" , $ this ->data ];
368
376
369
377
case 'select ' :
370
378
$ columns = $ this ->resolveColumns ();
@@ -375,28 +383,28 @@ public function resolve()
375
383
$ sort = $ this ->resolveSort ();
376
384
$ limit = $ this ->resolveLimit ();
377
385
378
- return ["SELECT $ columns FROM $ table {$ join }{$ where }{$ group }{$ having }{$ sort }{$ limit }; " , $ this ->data ];
386
+ return ["SELECT $ columns FROM $ table {$ join }{$ where }{$ group }{$ having }{$ sort }{$ limit }" , $ this ->data ];
379
387
380
388
case 'insert ' :
381
389
$ join = $ this ->resolveJoins ();
382
390
$ data = $ this ->resolveColumnData ();
383
391
$ limit = $ this ->resolveLimit ();
384
392
385
- return ["INSERT INTO $ table {$ join }{$ data }{$ limit }; " , $ this ->data ];
393
+ return ["INSERT INTO $ table {$ join }{$ data }{$ limit }" , $ this ->data ];
386
394
387
395
case 'update ' :
388
396
$ join = $ this ->resolveJoins ();
389
397
$ data = $ this ->resolveColumnData ();
390
398
$ where = $ this ->resolveWhere ();
391
399
$ limit = $ this ->resolveLimit ();
392
400
393
- return ["UPDATE $ table {$ join }{$ data }{$ where }{$ limit }; " , $ this ->data ];
401
+ return ["UPDATE $ table {$ join }{$ data }{$ where }{$ limit }" , $ this ->data ];
394
402
395
403
case 'delete ' :
396
404
$ where = $ this ->resolveWhere ();
397
405
$ limit = $ this ->resolveLimit ();
398
406
399
- return ["DELETE FROM $ table {$ where }{$ limit }; " , $ this ->data ];
407
+ return ["DELETE FROM $ table {$ where }{$ limit }" , $ this ->data ];
400
408
}
401
409
}
402
410
@@ -410,10 +418,19 @@ public function resolveJoins()
410
418
if (!$ this ->joins ) return '' ;
411
419
412
420
$ joinstring = '' ;
421
+
413
422
foreach ($ this ->joins as $ join )
414
423
{
415
- $ clauses = [];
416
- $ joinstring .= ' JOIN ' . $ this ->escapeTable ($ join ['table ' ]);
424
+ if ($ join ['table ' ]) {
425
+ $ clauses = [];
426
+ $ joinstring .= ' JOIN ' . $ this ->escapeTable ($ join ['table ' ]);
427
+ } elseif ($ join ['subquery ' ]) {
428
+ $ sql = $ join ['subquery ' ];
429
+ if ($ sql instanceof QueryBuilder) $ sql = $ join ['subquery ' ]->resolve ();
430
+
431
+ $ joinstring .= " JOIN ( $ sql) as {$ join ['alias ' ]}" ;
432
+ }
433
+
417
434
if ($ join ['where ' ])
418
435
{
419
436
foreach ($ join ['where ' ] as $ where ) {
0 commit comments