Skip to content

Commit ab2eff8

Browse files
committed
IMP nesting documentation
1 parent 721d5a5 commit ab2eff8

File tree

1 file changed

+79
-67
lines changed

1 file changed

+79
-67
lines changed

README.md

Lines changed: 79 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ 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)
30+
- [Nesting a Collection of Result Objects](#nesting-a-collection-of-result-objects)
31+
- [Nesting the First or Last Result Object](#nesting-the-first-or-last-result-object)
3332
- [Transactions ↑](#transactions-)
3433
- [Executing Stored Procedures ↑](#executing-stored-procedures-)
3534

@@ -440,7 +439,7 @@ due to increased loops and database access roundtrips. To address these
440439
challenges, a more efficient and lightweight syntax is available for querying
441440
nested data.
442441

443-
### Nesting a Collection of Result Objects (`nestMany`)
442+
### Nesting a Collection of Result Objects
444443

445444
The `nestMany` method is used to nest a collection of result objects within each
446445
row of the primary query's result. In the provided example, this is used to
@@ -451,7 +450,7 @@ suitable when you expect multiple related records for each main record.
451450
// Primary Query for Groups
452451
$groupsQuery = Query::selectFrom(
453452
'groups',
454-
['group_id', 'teacher_id', 'subject', 'classroom']
453+
['group_id', 'subject', 'classroom']
455454
);
456455

457456
// Query for Students
@@ -463,48 +462,58 @@ $studentsQuery = Query::selectFrom(
463462
// Nesting students within each group
464463
$groupsQuery
465464
->nestMany('Students', $studentsQuery, $groupRow, Student::class)
466-
->where('s.group_id', $groupRow->group_id);
465+
->where('students.group_id', $groupRow->group_id);
467466

468467
// Connecting to the database and executing the query
469468
$db = DatabaseManager::connect('school');
470469
$result = $db->executeSelect($groupsQuery);
471470
$groups = $result->toArray(Group::class);
472471
```
473472

474-
### Nestring the First Result Object (`linkFirst`)
473+
**Result:**
474+
475+
```json
476+
[
477+
{
478+
"group_id": 1,
479+
"subject": "Programing fundamentals",
480+
"classroom": "A113",
481+
"Students": [
482+
{
483+
"student_id": 325,
484+
"first_name": "Charlie",
485+
"last_name": "Ortega"
486+
},
487+
{
488+
"student_id": 743,
489+
"first_name": "Beth",
490+
"last_name": "Wilson"
491+
}
492+
]
493+
},
494+
{
495+
"group_id": 2,
496+
"subject" : "Object Oriented Programming",
497+
"classroom": "G7-R5",
498+
"Students": [
499+
{
500+
"student_id": 536,
501+
"first_name": "Dylan",
502+
"last_name": "Morrison"
503+
}
504+
]
505+
}
506+
]
507+
```
508+
509+
### Nesting the First or Last Result Object
475510

476511
On the other hand, the `linkFirst` method is employed to link only the first
477512
result object from a secondary query with each row of the primary query's
478513
result. In the given code snippet, this is used to link the first teacher to
479514
each group. This method is beneficial when you want to link a single related
480515
record to each main record, prioritizing the first match.
481516

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
490-
$teachersQuery = Query::selectFrom(
491-
'teachers',
492-
['teacher_id', 'first_name', 'last_name']
493-
);
494-
495-
// Linking the first teacher to each group
496-
$groupsQuery
497-
->linkFirst('Teacher', $teachersQuery, $groupRow, Teacher::class)
498-
->where('t.teacher_id', $groupRow->teacher_id);
499-
500-
// Connecting to the database and executing the query
501-
$db = DatabaseManager::connect('school');
502-
$result = $db->executeSelect($groupsQuery);
503-
$groups = $result->toArray(Group::class);
504-
```
505-
506-
### Nesting the last Result Object (`linkLast`)
507-
508517
Additionally, there is the `linkLast` method, which is similar to `linkFirst`
509518
but instead links the last result object from a secondary query to each row of
510519
the primary query's result. This can be useful in scenarios where you want to
@@ -523,53 +532,48 @@ $teachersQuery = Query::selectFrom(
523532
['teacher_id', 'first_name', 'last_name']
524533
);
525534

526-
// Linking the last teacher to each group
535+
// Linking the first teacher to each group
527536
$groupsQuery
528-
->linkLast('Teacher', $teachersQuery, $groupRow, Teacher::class)
529-
->where('t.teacher_id', $groupRow->teacher_id);
537+
->linkFirst('Teacher', $teachersQuery, $groupRow, Teacher::class)
538+
->where('teachers.teacher_id', $groupRow->teacher_id);
539+
540+
// Query for Classes
541+
$classesQuery = Query::selectFrom(
542+
'groups_classes',
543+
['class_id', 'group_id', 'topic', 'date']
544+
)->orderBy('date', 'ASC');
545+
546+
// Linking the last class to each group
547+
$groups
548+
->linkLast('LastClass', $classesQuery, $groupRow, GroupClass::class)
549+
->where('groups_classes.group_id', $groupRow->group_id);
530550

531551
// Connecting to the database and executing the query
532552
$db = DatabaseManager::connect('school');
533553
$result = $db->executeSelect($groupsQuery);
534554
$groups = $result->toArray(Group::class);
535555
```
536556

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-
541-
> **Old Nest Syntax**
542-
> ```php
543-
> $groupsQuery->nest(['Students' => $studentsQuery], function (NestedSelect $nest, RowProxy $row) {
544-
> $nest->getSelect()->where('s.group_id', $row->group_id);
545-
> }, NestMode::COLLECTION, Student::class);
546-
> ```
557+
**Result:**
547558

548-
Result would be like this:
549559
```json
550560
[
551561
{
552562
"group_id": 1,
553563
"teacher_id": 3,
554-
"subject": "Programing fundamentals",
564+
"subject": "Programming fundamentals",
555565
"classroom": "A113",
556566
"Teacher": {
557567
"teacher_id": 3,
558568
"first_name": "Rosemary",
559569
"last_name": "Smith"
560570
},
561-
"Students": [
562-
{
563-
"student_id": 325,
564-
"first_name": "Charlie",
565-
"last_name": "Ortega"
566-
},
567-
{
568-
"student_id": 743,
569-
"first_name": "Beth",
570-
"last_name": "Wilson"
571-
}
572-
]
571+
"LastClass": {
572+
"class_id": 233,
573+
"group_id": 1,
574+
"topic": "Algorithms",
575+
"date": "2024-04-18"
576+
}
573577
},
574578
{
575579
"group_id": 2,
@@ -581,17 +585,25 @@ Result would be like this:
581585
"first_name": "Steve",
582586
"last_name": "Johnson"
583587
},
584-
"Students": [
585-
{
586-
"student_id": 536,
587-
"first_name": "Dylan",
588-
"last_name": "Morrison"
589-
}
590-
]
588+
"LastClass": null
591589
}
592590
]
593591
```
594592

593+
By choosing the appropriate nesting mode (`nestMany`, `linkFirst`, or
594+
`linkLast`), you can tailor your queries to efficiently handle nested data based
595+
on your specific data structure and requirements.
596+
597+
> **Legacy old nest syntax**
598+
>
599+
> There is a legacy nest syntax, that stills working underhood.
600+
>
601+
> ```php
602+
> $groupsQuery->nest(['Students' => $studentsQuery], function (NestedSelect $nest, RowProxy $row) {
603+
> $nest->getSelect()->where('s.group_id', $row->group_id);
604+
> }, NestMode::COLLECTION, Student::class);
605+
> ```
606+
595607
Transactions [↑](#table-of-contents)
596608
---------------------------------------
597609

0 commit comments

Comments
 (0)