PHPackages                             philippgrashoff/mtomforatk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. philippgrashoff/mtomforatk

ActiveLibrary

philippgrashoff/mtomforatk
==========================

6.0.0(1y ago)11.4k↓31%3PHPPHP 8.\*CI failing

Since Mar 11Pushed 1y ago1 watchersCompare

[ Source](https://github.com/PhilippGrashoff/mtomforatk)[ Packagist](https://packagist.org/packages/philippgrashoff/mtomforatk)[ RSS](/packages/philippgrashoff-mtomforatk/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (3)Versions (10)Used By (3)

mtomforatk
==========

[](#mtomforatk)

[![codecov](https://camo.githubusercontent.com/0ac4fcbc4f5435eeeebf081253d87096685b9c6cd16e968d7062179c764af448/68747470733a2f2f636f6465636f762e696f2f67682f5068696c69707047726173686f66662f6d746f6d666f7261746b2f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/PhilippGrashoff/mtomforatk)

An addition to [atk4/data](https://github.com/atk4/data) to easily manage Many To Many (MToM) Relations. The purpose is to write as little code as possible for actual MToM operations.

Project Content
===============

[](#project-content)

The project consists of two files:

- **JunctionModel**: A base model for a junction class (like StudentToLesson). Working descendants can be coded with a few lines of code. Static methods to add (e.g. `StudentToLesson::addMToMRelation($student, $lesson);`), remove ((e.g. `StudentToLesson::removeMToMRelation($student, $lesson);`) and check ((e.g. `StudentToLesson::hasMToMRelation($student, $lesson);`) are implemented here.
- **MToMTrait**: A Trait which is added to the models to be linked, (like Student and Lesson). With this trait, the MToM relation can be defined with a single line in Model::init(): `$this->addMToMReferenceAndDeleteHook();`.

How to use
==========

[](#how-to-use)

Installation
------------

[](#installation)

The easiest way to use this repository is to add it to your composer.json in the requirement section:

```
{
  "require": {
    "philippgrashoff/mtomforatk": "4.0.*"
  }
}
```

Sample code
-----------

[](#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 uses MToMTrait.
- Lesson: A normal model which uses MToMTrait.
- StudentToLesson: The junction 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:

```
