Skip to content

Commit 721d5a5

Browse files
committed
IMP documentation for nestMany, linkFirst and linkLast
1 parent adcccd0 commit 721d5a5

File tree

1 file changed

+83
-17
lines changed

1 file changed

+83
-17
lines changed

README.md

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Table of contents
2727
- [SUPPORTED JOIN TYPES](#supported-join-types)
2828
- [Examples](#examples)
2929
- [SELECT nesting ↑](#select-nesting-)
30+
- [Nesting a Collection of Result Objects (`nestMany`)](#nesting-a-collection-of-result-objects-nestmany)
31+
- [Nestring the First Result Object (`linkFirst`)](#nestring-the-first-result-object-linkfirst)
32+
- [Nesting the last Result Object (`linkLast`)](#nesting-the-last-result-object-linklast)
3033
- [Transactions ↑](#transactions-)
3134
- [Executing Stored Procedures ↑](#executing-stored-procedures-)
3235

@@ -428,53 +431,116 @@ $query->columns([
428431
SELECT nesting [](#table-of-contents)
429432
---------------------------------------
430433

431-
Sometimes database table joining might not be enought for all the data requirements.
432-
Is quite often that for each row in a result of a query, another filtered result
433-
query must be executed.
434+
In some cases, simple database table joining isn't sufficient for meeting all
435+
data requirements. It's common to need to execute additional filtered queries
436+
for each row in the result of a primary query.
434437

435-
This scenario produces excesive complex code to nest each result by each row.
436-
Also impacts performance by increasing the loops and database access roundtrips.
437-
For this reason there's a syntax that creates the most lightweight and efficient
438-
way to query nested data, preventing access overhead and reducing processing time.
438+
However, this approach often leads to overly complex code and performance issues
439+
due to increased loops and database access roundtrips. To address these
440+
challenges, a more efficient and lightweight syntax is available for querying
441+
nested data.
439442

440-
```php
443+
### Nesting a Collection of Result Objects (`nestMany`)
444+
445+
The `nestMany` method is used to nest a collection of result objects within each
446+
row of the primary query's result. In the provided example, this is used to
447+
associate multiple students with their respective groups. This approach is
448+
suitable when you expect multiple related records for each main record.
441449

442-
// Groups query
450+
```php
451+
// Primary Query for Groups
443452
$groupsQuery = Query::selectFrom(
444453
'groups',
445454
['group_id', 'teacher_id', 'subject', 'classroom']
446455
);
447456

448-
// NESTING A COLLECTION OF RESULT OBJECTS
449-
// Students query
457+
// Query for Students
450458
$studentsQuery = Query::selectFrom(
451459
'students',
452460
['student_id', 'group_id', 'first_name', 'last_name']
453461
);
454-
// Nesting students by each group
462+
463+
// Nesting students within each group
455464
$groupsQuery
456465
->nestMany('Students', $studentsQuery, $groupRow, Student::class)
457466
->where('s.group_id', $groupRow->group_id);
458467

459-
// NESTING THE FIRST RESULT OBJECT
460-
// Teachers Query
468+
// Connecting to the database and executing the query
469+
$db = DatabaseManager::connect('school');
470+
$result = $db->executeSelect($groupsQuery);
471+
$groups = $result->toArray(Group::class);
472+
```
473+
474+
### Nestring the First Result Object (`linkFirst`)
475+
476+
On the other hand, the `linkFirst` method is employed to link only the first
477+
result object from a secondary query with each row of the primary query's
478+
result. In the given code snippet, this is used to link the first teacher to
479+
each group. This method is beneficial when you want to link a single related
480+
record to each main record, prioritizing the first match.
481+
482+
```php
483+
// Primary Query for Groups
484+
$groupsQuery = Query::selectFrom(
485+
'groups',
486+
['group_id', 'teacher_id', 'subject', 'classroom']
487+
);
488+
489+
// Query for Teachers
461490
$teachersQuery = Query::selectFrom(
462491
'teachers',
463492
['teacher_id', 'first_name', 'last_name']
464493
);
465-
// Nest-link teacher by each group
494+
495+
// Linking the first teacher to each group
466496
$groupsQuery
467497
->linkFirst('Teacher', $teachersQuery, $groupRow, Teacher::class)
468498
->where('t.teacher_id', $groupRow->teacher_id);
469499

500+
// Connecting to the database and executing the query
470501
$db = DatabaseManager::connect('school');
471502
$result = $db->executeSelect($groupsQuery);
472-
$groups = $result->toArray();
503+
$groups = $result->toArray(Group::class);
473504
```
474505

506+
### Nesting the last Result Object (`linkLast`)
507+
508+
Additionally, there is the `linkLast` method, which is similar to `linkFirst`
509+
but instead links the last result object from a secondary query to each row of
510+
the primary query's result. This can be useful in scenarios where you want to
511+
prioritize the most recent or latest related record for each main record.
512+
513+
```php
514+
// Primary Query for Groups
515+
$groupsQuery = Query::selectFrom(
516+
'groups',
517+
['group_id', 'teacher_id', 'subject', 'classroom']
518+
);
519+
520+
// Query for Teachers
521+
$teachersQuery = Query::selectFrom(
522+
'teachers',
523+
['teacher_id', 'first_name', 'last_name']
524+
);
525+
526+
// Linking the last teacher to each group
527+
$groupsQuery
528+
->linkLast('Teacher', $teachersQuery, $groupRow, Teacher::class)
529+
->where('t.teacher_id', $groupRow->teacher_id);
530+
531+
// Connecting to the database and executing the query
532+
$db = DatabaseManager::connect('school');
533+
$result = $db->executeSelect($groupsQuery);
534+
$groups = $result->toArray(Group::class);
535+
```
536+
537+
By choosing the appropriate nesting mode (`nestMany`, `linkFirst`, or
538+
`linkLast`), you can tailor your queries to efficiently handle nested data based
539+
on your specific data structure and requirements.
540+
475541
> **Old Nest Syntax**
476542
> ```php
477-
> $groupsQuery->nest(['Students' => $studentsQuery], function > (NestedSelect $nest, RowProxy $row) {
543+
> $groupsQuery->nest(['Students' => $studentsQuery], function (NestedSelect $nest, RowProxy $row) {
478544
> $nest->getSelect()->where('s.group_id', $row->group_id);
479545
> }, NestMode::COLLECTION, Student::class);
480546
> ```

0 commit comments

Comments
 (0)