Skip to content

Commit 8579419

Browse files
constpb2394roxblnfk
authored andcommitted
docs: cover SubQuery with comments
1 parent 02484a6 commit 8579419

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Injection/SubQuery.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@
1616
use Cycle\Database\Query\QueryParameters;
1717
use Cycle\Database\Query\SelectQuery;
1818

19+
/**
20+
* This fragment is used to inject a whole select statement into
21+
* FROM and SELECT parts of the query.
22+
*
23+
* Examples:
24+
*
25+
* ```
26+
* $subQuery = new SubQuery($queryBuilder->select()->from(['users']),'u');
27+
* $query = $queryBuilder->select()->from($subQuery);
28+
* ```
29+
*
30+
* Will provide SQL like this: SELECT * FROM (SELECT * FROM users) AS u
31+
*
32+
* ```
33+
* $subQuery = new SubQuery($queryBuilder->select()->from(['users']),'u');
34+
* $query = $queryBuilder->select($subQuery)->from(['employee']);
35+
* ```
36+
*
37+
* Will provide SQL like this: SELECT *, (SELECT * FROM users) AS u FROM employee
38+
*/
1939
class SubQuery implements FragmentInterface
2040
{
2141
private SelectQuery $query;

src/Query/SelectQuery.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Cycle\Database\Injection\Expression;
1515
use Cycle\Database\Injection\Fragment;
16+
use Cycle\Database\Injection\SubQuery;
1617
use Cycle\Database\Query\Traits\WhereJsonTrait;
1718
use Cycle\Database\Driver\CompilerInterface;
1819
use Cycle\Database\Injection\FragmentInterface;
@@ -84,6 +85,16 @@ public function distinct(bool|string|FragmentInterface $distinct = true): self
8485
/**
8586
* Set table names SELECT query should be performed for. Table names can be provided with
8687
* specified alias (AS construction).
88+
* Also, it is possible to use SubQuery.
89+
*
90+
* Following example will provide SQL like this: SELECT * FROM (SELECT * FROM users) AS u
91+
*
92+
* ```
93+
* $subQuery = new SubQuery($queryBuilder->select()->from(['users']),'u');
94+
* $query = $queryBuilder->select()->from($subQuery);
95+
* ```
96+
*
97+
* @see SubQuery
8798
*/
8899
public function from(mixed $tables): self
89100
{

0 commit comments

Comments
 (0)