philippgrashoff / mtomforatk
此包的最新版本(5.2.0)没有可用的许可证信息。
5.2.0
2024-06-23 06:24 UTC
Requires
- php: 8.*
- atk4/data: 5.*
Requires (Dev)
- phpstan/phpstan: 1.*
- phpunit/phpunit: ^9.5.25
README
是 atk4/data 的扩展,以便轻松管理多对多(MToM)关系。目的是在实际的多对多操作中编写尽可能少的代码。
项目内容
该项目包含两个文件
- JunctionModel:一个连接类的基模型(如StudentToLesson)。可以通过几行代码编写工作后代码。在这里实现了添加(例如,
StudentToLesson::addMToMRelation($student, $lesson);
)、删除(例如,StudentToLesson::removeMToMRelation($student, $lesson);
)和检查(例如,StudentToLesson::hasMToMRelation($student, $lesson);
)的静态方法。 - MToMTrait:一个特质,用于添加到要链接的模型中(如Student和Lesson)。使用此特质,可以在Model::init()中用一行代码定义MToM关系:
$this->addMToMReferenceAndDeleteHook();
。
如何使用
安装
使用此存储库的最简单方法是将其添加到composer.json的require部分
{ "require": { "philippgrashoff/mtomforatk": "4.0.*" } }
示例代码
例如,让我们使用Students和Lessons。一个学生可以有多个课程,一个课程可以有多个学生。为了映射这种多对多关系,创建了3个类。此示例的演示模型可以在tests\Testmodels中找到
- Student:一个使用MToMTrait的正常模型。
- Lesson:一个使用MToMTrait的正常模型。
- StudentToLesson:携带每个学生和课程之间的MToM关系的student_id和lesson_id的连接模型。
设置这些类后,可以使用此项目轻松执行MToM操作
<?php declare(strict_types=1); use Atk4\Data\Persistence\Sql; use PhilippR\Atk4\MToM\Tests\Testmodels\Lesson; use PhilippR\Atk4\MToM\Tests\Testmodels\Student; use PhilippR\Atk4\MToM\Tests\Testmodels\StudentToLesson; $persistence = new 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: StudentToLesson::addMToMRelation($studentHarry, $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 passing the other model or only it's ID. In this case, we just pass the ID - that's what you typically get from UI StudentToLesson::addMToMRelation($studentHarry, $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 StudentToLesson::hasMToMRelation($studentHarry, $lessonGeography); //true; //harry is tired of Geography, lets remove him from this lesson: StudentToLesson::removeMToMRelation($studentHarry, $lessonGeography); //this removed the StudentToLesson Record linking Harry to Geography. StudentToLesson::hasMToMRelation($studentHarry, $lessonGeography); //false //Linda attends both courses. Let's add Linda to both courses. But this time we do it the other way around and pass the lesson model as first argument: $studentLinda = (new Student($persistence))->createEntity(); $studentLinda->set('name', 'Linda'); $studentLinda->save(); StudentToLesson::addMToMRelation($lessonGeography, $studentLinda); StudentToLesson::addMToMRelation($lessonBiology, $studentLinda);
此README中的示例代码可以在docs
目录中找到。
版本控制
此存储库的版本号与atk4\data版本相对应。因此,4.0.x与atk4\data 4.0.x兼容,依此类推。