PHPackages                             tatter/roster - 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. [Database &amp; ORM](/categories/database)
4. /
5. tatter/roster

ActiveLibrary[Database &amp; ORM](/categories/database)

tatter/roster
=============

Bulk name lookup for database relations in CodeIgniter 4

v1.0.1(4y ago)63.8k2[1 issues](https://github.com/tattersoftware/codeigniter4-roster/issues)[1 PRs](https://github.com/tattersoftware/codeigniter4-roster/pulls)MITPHPPHP ^7.4 || ^8.0

Since Sep 28Pushed 2y ago2 watchersCompare

[ Source](https://github.com/tattersoftware/codeigniter4-roster)[ Packagist](https://packagist.org/packages/tatter/roster)[ Docs](https://github.com/tattersoftware/codeigniter4-roster)[ Fund](https://paypal.me/tatter)[ GitHub Sponsors](https://github.com/tattersoftware)[ RSS](/packages/tatter-roster/feed)WikiDiscussions develop Synced 3d ago

READMEChangelog (2)Dependencies (2)Versions (5)Used By (0)

Tatter\\Roster
==============

[](#tatterroster)

Bulk name lookup for database relations in CodeIgniter 4

[![](https://github.com/tattersoftware/codeigniter4-roster/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-roster/actions/workflows/test.yml)[![](https://github.com/tattersoftware/codeigniter4-roster/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-roster/actions/workflows/analyze.yml)[![](https://github.com/tattersoftware/codeigniter4-roster/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-roster/actions/workflows/inspect.yml)[![Coverage Status](https://camo.githubusercontent.com/9aac28e39e835be85da5995162066ae7ada10d0082b47800078245c883599e50/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f746174746572736f6674776172652f636f646569676e69746572342d726f737465722f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/tattersoftware/codeigniter4-roster?branch=develop)

Quick Start
-----------

[](#quick-start)

1. Install with Composer: `> composer require tatter/roster`
2. Create a Roster class
3. Load high-performance names: ``

Description
-----------

[](#description)

`Roster` solves a common, niche problem in an elegant way: quick access to display names for entity relations without requiring database lookup. An example... Your e-commerce app allows users to list their own products along with their username. To display the full product page, traditionally you would either need a database `JOIN` to fetch the usernames along with each product, or rely on a third-party solution like Object Relation Mapping (ORM) to load the related information. `Roster` simplifies and optimizes this by preloading batches of object names and caching them for convenient on-the-fly access.

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

[](#installation)

Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities and always be up-to-date:

- `> composer require tatter/roster`

Or, install manually by downloading the source files and adding the directory to `app/Config/Autoload.php`.

Usage
-----

[](#usage)

The `Roster` service handles locating and interacting with your Roster classes, so all you need to do is create some Rosters. All Rosters must meet a few criteria to be discovered:

- Rosters must extend the Base Roster (`Tatter\Roster\BaseRoster`)
- Rosters must be located in a **Rosters** folder within a namespace (e.g. `App\Rosters`)
- Rosters must be named by their lookup followed "Roster" (e.g. "CarRoster")

### BaseRoster

[](#baseroster)

`BaseRoster` defines the three methods that your class must implement:

- `protected function key(): string;`
- `protected function fetchAll(): array;`
- `protected function fetch($id): ?string;`

*See the `BaseRoster` file for more details.*

### ModelRoster

[](#modelroster)

Most of the time Rosters will be fetching information from the database. In order to make this more convenient and reduce repetitive code this library comes with an intermediate support class, `ModelRoster`. If your Roster aligns with an existing Model then simply extend the `ModelRoster` class and supply these required fields:

- `protected $modelName;`
- `protected $field;`

### Displaying

[](#displaying)

Once your Rosters are configured, use the service with the Roster name as the method and the ID of the item as the sole parameter:

```
$userName = service('roster')->user($userId);
```

Example
-------

[](#example)

You are developing a blog. At the bottom of every post is a comments section where logged in users may post replies. Being the bright developer you are, you decide to use `Tatter\Roster`to handle the display and save on expensive database joins for every page.

First let's handle displaying the username next to each commet. You already have `UserModel`so we can use the `ModelRoster` to make it easier. Create **app/Rosters/UserRoster.php**:

```
namespace App\Rosters;

use App\Models\UserModel;
use Tatter\Roster\ModelRoster;

class UserRoster extends ModelRoster
{
	protected $modelName = UserModel::class;
	protected $field     = 'username';
}
```

That's it! `ModelRoster` handles retrieving the values based those properties. Now in our comment HTML block we can use the Roster service to display each username:

```

        Commented by
