11# Tatter\Relations
22Entity relationships for CodeIgniter 4
33
4- [ ![ ] ( https://github.com/tattersoftware/codeigniter4-relations/workflows/PHPUnit/badge.svg )] ( https://github.com/tattersoftware/codeigniter4-relations/actions?query=workflow%3A%22PHPUnit )
5- [ ![ ] ( https://github.com/tattersoftware/codeigniter4-relations/workflows/PHPStan/badge.svg )] ( https://github.com/tattersoftware/codeigniter4-relations/actions?query=workflow%3A%22PHPStan )
4+ [ ![ ] ( https://github.com/tattersoftware/codeigniter4-relations/workflows/PHPUnit/badge.svg )] ( https://github.com/tattersoftware/codeigniter4-relations/actions/workflows/phpunit.yml )
5+ [ ![ ] ( https://github.com/tattersoftware/codeigniter4-relations/workflows/PHPStan/badge.svg )] ( https://github.com/tattersoftware/codeigniter4-relations/actions/workflows/phpstan.yml )
6+ [ ![ ] ( https://github.com/tattersoftware/codeigniter4-relations/workflows/Deptrac/badge.svg )] ( https://github.com/tattersoftware/codeigniter4-relations/actions/workflows/deptrac.yml )
67[ ![ Coverage Status] ( https://coveralls.io/repos/github/tattersoftware/codeigniter4-relations/badge.svg?branch=develop )] ( https://coveralls.io/github/tattersoftware/codeigniter4-relations?branch=develop )
78
89## Quick Start
@@ -19,10 +20,12 @@ Entity relationships for CodeIgniter 4
1920
2021Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities
2122and always be up-to-date:
22- * ` > composer require tatter/relations `
23+ ``` shell
24+ > composer require tatter/relations
25+ ```
2326
2427Or, install manually by downloading the source files and adding the directory to
25- ` app/Config/Autoload.php ` .
28+ ** app/Config/Autoload.php*** .
2629
2730## Configuration (optional)
2831
@@ -50,21 +53,20 @@ methods and injecting relations into the returned results. Because this happens
5053level, related items can be loaded ahead of time in batches ("eager loading").
5154
5255Add the trait to your models:
53-
56+ ``` php
5457 use \Tatter\Relations\Traits\ModelTrait
58+ ```
5559
5660Related items can be requested by adding a ` $with ` property to your model:
57-
58- ```
61+ ``` php
5962 protected $with = 'groups';
6063 // or
6164 protected $with = ['groups', 'permissions'];
6265```
6366
6467... or by requesting it on-the-fly using the model ` with() ` method:
6568
66-
67- ```
69+ ``` php
6870$users = $userModel->with('groups')->findAll();
6971foreach ($users as $userEntity)
7072{
@@ -82,17 +84,15 @@ and `__call()` methods to check for matching database tables. Because this happe
8284item, related items can be retrieved or updated on-the-fly ("lazy loading").
8385
8486Add the trait and its necessary properties to your entities:
85-
86- ```
87+ ``` php
8788 use \Tatter\Relations\Traits\EntityTrait
8889
8990 protected $table = 'users';
9091 protected $primaryKey = 'id';
9192```
9293
9394Related items are available as faux properties:
94-
95- ```
95+ ``` php
9696 $user = $userModel->find(1);
9797
9898 foreach ($user->groups as $group)
@@ -102,8 +102,7 @@ Related items are available as faux properties:
102102```
103103
104104... and can also be updated directly from the entity:
105-
106- ```
105+ ``` php
107106 $user->addGroup(3);
108107
109108 if ($user->hasGroups([1, 3]))
@@ -123,7 +122,7 @@ for "manyToMany" relationships.
123122successful, ** Relations** will use each table's model to find the related items. This keeps
124123consistent the return types, events, and other aspects of your models. In addition to the
125124return type, ** Relations** will also adjust related items for singleton relationships:
126- ```
125+ ``` php
127126// User hasMany Widgets
128127$user = $userModel->with('widgets')->find($userId);
129128echo "User {$user->name} has " . count($user->widgets) . " widgets.";
@@ -138,7 +137,7 @@ echo $widget->name . " belongs to " . $widget->user->name;
138137** ModelTrait** supports nested relation calls, but these can be resource intensive so may
139138be disabled by changing ` $allowNesting ` in the config. With nesting enabled, any related
140139items will also load their related items (but not infinitely):
141- ```
140+ ``` php
142141/* Define your models */
143142class UserModel
144143{
@@ -178,7 +177,7 @@ request initiates the load. The schema will attempt to cache to prevent this del
178177if your cache is not configured correctly you will likely experience noticeable performance
179178degradation. The recommended approach is to have a cron job generate your schema regularly
180179so it never expires and no user will trigger the un-cached load, e.g.:
181- ```
180+ ``` shell
182181php spark schemas
183182```
184183
0 commit comments