-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
save WIP: namespace, better readme, better class names
- Loading branch information
1 parent
5b84186
commit 40dfd53
Showing
13 changed files
with
175 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,56 @@ | ||
# mtomforatk | ||
[![codecov](https://codecov.io/gh/PhilippGrashoff/mtomforatk/branch/master/graph/badge.svg)](https://codecov.io/gh/PhilippGrashoff/mtomforatk) | ||
|
||
An addition to atk4/data to easily manage Many To Many (MToM) Relations. The purpose | ||
is to write as little code as possible for actual MToM operations. | ||
|
||
## Example code | ||
As Example, lets use Students and Lessons. A Teacher can have many Lessons, a Lesson can have many Teachers. | ||
To map this MToM relationship, 3 classes are created: | ||
* Teacher | ||
* Lesson | ||
* TeacherToLesson | ||
|
||
After setting these classes up using this project, MToM operations can be done easily: | ||
``` | ||
$teacher = (new Teacher($persistence))->createEntity(); | ||
$teacher->save(); | ||
//Add Lesson by its ID, in this case 123 | ||
$teacherToLesson = $teacher->addMToMRelation(new TeacherToLesson($persistence), 123); //creates a new TeacherToLesson record | ||
$lessonWithId123 = $teacherToLesson->getReferencedEntity(Lesson::class); //easy way to get Lesson object. No extra DB query is used. | ||
//remove lesson by its ID | ||
$teacher->removeMToMRelation(new TeacherToLesson($persistence), 123); //removes the TeacherToLesson record | ||
//Add a lesson by passing the Entity | ||
$lesson = (new Lesson($persistence))->createEntity(); | ||
$lesson->save(); | ||
$teacher->addMToMRelation(new TeacherToLesson($persistence), $lesson); | ||
$teacher->hasMToMRelation(new TeacherToLesson($persistence), $lesson); //true | ||
//remove a lesson by passing object | ||
$teacher->removeMToMRelation(new TeacherToLesson($persistence), $lesson); | ||
$teacher->hasMToMRelation(new TeacherToLesson($persistence), $lesson); //falses | ||
``` | ||
|
||
If you want even more comfort, implement some wrapper functions which further shorten the code. | ||
As Example, another MToMRelation is set up in test/testmodels: StudentToLesson. A Student can | ||
have many Lessons, a Lesson can have many Students: | ||
* Student | ||
* StudentToLesson | ||
|
||
See Student where addLesson(), removeLesson() and hasLessonRelation() wrapper functions are implemented: | ||
``` | ||
$student->addLesson($lesson); | ||
$student->hasLessonRelation($lesson); //true | ||
$student->removeLession($lesson); | ||
$student->hasLessonRelation($lesson); //false | ||
``` | ||
|
||
## Project Content | ||
# Project Content | ||
The project consists of two files: | ||
* MToMModel: A base model for the intermediate class (like StudentToLesson). Working descendants can be coded with a few lines of code. | ||
* MToMRelationForModelTrait: A Trait which is added to the models to be linked, (like Student and Lesson). With this trait, only a few more lines need to be added to make operations like `$lesson->addStudent(5);` work. | ||
|
||
For an example implementation, have a look at tests/testmodels. Here you can find Student, Lesson and StudentToLesson Models. | ||
# How to use | ||
## Installation | ||
The easiest way to use this repository is to add it to your composer.json in the require section: | ||
```json | ||
{ | ||
"require": { | ||
"philippgrashoff/cronforatk": "4.0.*" | ||
} | ||
} | ||
``` | ||
## Sample code | ||
As example, lets use Students and Lessons. A Student can have many Lessons, a Lesson can have many Students. | ||
To map this MToM relationship, 3 classes are created. Demo models for this example can be found in tests\Testmodels: | ||
* Student: A normal model which additionally uses ModelWithMToMTrait. 3 little helpers methods are implemented to make MToM handling easier: addLesson(), removeLesson() and hasLesson(); | ||
* Lesson: A normal model which additionally uses ModelWithMToMTrait. 3 little helpers methods are implemented to make MToM handling easier: addStudent(), removeStudent() and hasStudent(); | ||
* StudentToLesson: The intermediate model carrying the student_id and lesson_id for each MToM relation between Students and Lessons. | ||
|
||
After setting these classes up using this project, MToM operations can be done easily: | ||
```php | ||
$studentHarry = (new Student($persistence))->createEntity(); | ||
$studentHarry->set('name', 'Harry'); | ||
$studentHarry->save(); | ||
$lessonGeography = (new Lesson($persistence))->createEntity(); | ||
$lessonGeography->set('name', 'Geography'); | ||
$lessonGeography->save(); | ||
|
||
//now, lets easily add Harry to the Geography lesson: | ||
$studentHarry->addLesson($lessonGeography); | ||
//the above line created a StudentToLesson record with student_id = studentHarry's ID and lesson_id = lessonGeography's ID | ||
|
||
//let's add Harry to another lesson | ||
$lessonBiology = (new Lesson($persistence))->createEntity(); | ||
$lessonBiology->set('name', 'Biology'); | ||
$lessonBiology->save(); | ||
//adding/removing can either be done by passong the other model or only it's ID. In this case, we just pass the ID | ||
$studentHarry->addLesson($lessonBiology->getId()); | ||
//this created another StudentToLesson record with student_id = studentHarry's ID and lesson_id = lessonBiology's ID | ||
|
||
//Let's easily check if an MToM relation exists | ||
$studentHarry->hasLesson($lessonGeography); //true; | ||
|
||
//harry is tired of Geography, lets remove him from this lesson: | ||
$studentHarry->removeLesson($lessonGeography); | ||
//this removed the StudentToLesson Record linking Harry to Geography. | ||
$studentHarry->hasLesson($lessonGeography); //false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use PhilippR\Atk4\MToM\Tests\Testmodels\Lesson; | ||
use PhilippR\Atk4\MToM\Tests\Testmodels\Student; | ||
|
||
$persistence = new \Atk4\Data\Persistence\Sql('sqlite::memory:'); | ||
|
||
$studentHarry = (new Student($persistence))->createEntity(); | ||
$studentHarry->set('name', 'Harry'); | ||
$studentHarry->save(); | ||
$lessonGeography = (new Lesson($persistence))->createEntity(); | ||
$lessonGeography->set('name', 'Geography'); | ||
$lessonGeography->save(); | ||
|
||
//now, lets easily add Harry to the Geography lesson: | ||
$studentHarry->addLesson($lessonGeography); | ||
//the above line created a StudentToLesson record with student_id = studentHarry's ID and lesson_id = lessonGeography's ID | ||
|
||
//let's add Harry to another lesson | ||
$lessonBiology = (new Lesson($persistence))->createEntity(); | ||
$lessonBiology->set('name', 'Biology'); | ||
$lessonBiology->save(); | ||
//adding/removing can either be done by passong the other model or only it's ID. In this case, we just pass the ID | ||
$studentHarry->addLesson($lessonBiology->getId()); | ||
//this created another StudentToLesson record with student_id = studentHarry's ID and lesson_id = lessonBiology's ID | ||
|
||
//Let's easily check if an MToM relation exists | ||
$studentHarry->hasLesson($lessonGeography); //true; | ||
|
||
//harry is tired of Geography, lets remove him from this lesson: | ||
$studentHarry->removeLesson($lessonGeography); | ||
//this removed the StudentToLesson Record linking Harry to Geography. | ||
$studentHarry->hasLesson($lessonGeography); //false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.