Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/michalsn/codeigniter-nested-model

Dead simple nested model relations for CodeIgniter 4 framework
https://github.com/michalsn/codeigniter-nested-model

codeigniter codeigniter4 database-relations model

Last synced: 3 months ago
JSON representation

Dead simple nested model relations for CodeIgniter 4 framework

Awesome Lists containing this project

README

        

# CodeIgniter Nested Model

Dead simple nested model relations for CodeIgniter 4 framework. Relations are eager loaded. Each relation is one additional database query.

> [!WARNING]
> This project is experimental.

### Example

```php
// app/Models/UserModel.php
['hasOne', AvatarModel::class], //'user_id', 'id'],
// many social links - relation type, model, foreign key, local key
'links' => ['hasMany', LinkModel::class], //'user_id', 'id'],
];
}
```
```php
// app/Config/Routes.php
get('nested', static function () {

// get all users with avatar and links
d(model(UserModel::class)->with('avatar')->with('links')->findAll());

// get user with id = 2 and all links
d(model(UserModel::class)->with('links')->find(2));

// get user with id = 2 and links with type 'test'
d(model(UserModel::class)->with('links', static function () {
return model(LinkModel::class)->where('type', 'test');
})->find(2));

});

...
```