Download the PHP package tatter/relations without Composer

On this page you can find all versions of the php package tatter/relations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package relations

Tatter\Relations

Entity relationships for CodeIgniter 4

Coverage Status

Quick Start

  1. Install with Composer: > composer require tatter/relations
  2. Add the trait to your model: use \Tatter\Relations\Traits\ModelTrait
  3. Load relations: $users = $userModel->with('groups')->findAll();
  4. Add the trait to your entity: use \Tatter\Relations\Traits\EntityTrait
  5. Load relations: foreach ($user->groups as $group)

(See also Examples at the bottom)

Installation

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

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

Configuration (optional)

The library's default behavior can be altered by extending its config file. Copy examples/Relations.php to app/Config/ and follow the instructions in the comments. If no config file is found in app/Config the library will use its own.

Schemas

All the functionality of the library relies on the generated database schema. The schema comes from Tatter\Schemas and can be adjusted based on your needs (see the Schemas config file). If you want to use the auto-generated schema your database will have follow conventional naming patterns for foreign keys and pivot/join tables; see Tatter\Schemas for details.

Usage

Relation loading is handled by traits that are added to their respective elements.

Eager/Model

ModelTrait adds relation loading to your models by extending the default model find* methods and injecting relations into the returned results. Because this happens at the model level, related items can be loaded ahead of time in batches ("eager loading").

Add the trait to your models:

Related items can be requested by adding a $with property to your model:

... or by requesting it on-the-fly using the model with() method:

As you can see the related items are added directly to their corresponding object (or array) returned from the framework's model.

Lazy/Entity

EntityTrait adds relation loading to individual items by extending adding magic __get() and __call() methods to check for matching database tables. Because this happens on each item, related items can be retrieved or updated on-the-fly ("lazy loading").

Add the trait and its necessary properties to your entities:

Related items are available as faux properties:

... and can also be updated directly from the entity:

Available magic method verbs are: has, set, add, and remove, and are only applicable for "manyToMany" relationships.

Returned items

Schemas will attempt to associate your database tables back to their models, and if successful, Relations will use each table's model to find the related items. This keeps consistent the return types, events, and other aspects of your models. In addition to the return type, Relations will also adjust related items for singleton relationships:

Nesting

ModelTrait supports nested relation calls, but these can be resource intensive so may be disabled by changing $allowNesting in the config. With nesting enabled, any related items will also load their related items (but not infinitely):

Soft Deletes

If your target relations correspond to a CodeIgniter Model that uses soft deletion then you may include the table name in the array $withDeletedRelations property to include soft deleted items. This is particularly helpful for tight relationships, like when an item belongsTo another item that has been soft deleted. $withDeletedRelations works on both Entities and Models.

Performance

WARNING: Be aware that Relations relies on a schema generated from the Schemas library. While this process is relatively quick, it will cause a noticeable delay if a page request initiates the load. The schema will attempt to cache to prevent this delay, but if your cache is not configured correctly you will likely experience noticeable performance degradation. The recommended approach is to have a cron job generate your schema regularly so it never expires and no user will trigger the un-cached load, e.g.:

See Tatter\Schemas for more details.

Eager or Lazy Loading

You are responsible for your application's performance! These tools are here to help, but they still allow dumb things.

Eager loading (via ModelTrait) can create a huge performance increase by consolidating what would normally be multiple database calls into one. However, the related items will take up additional memory and can cause other bottlenecks or script failures if used indiscriminately.

Lazy loading (via EntityTrait) makes it very easy to work with related items only when they are needed, and the magic functions keep your code clear and concise. However, each entity issues its own database call and can really start to slow down performance if used over over.

A good rule of thumb is to use ModelTrait to preload relations that will be handled repeatedly (e.g. in loops) or that represent a very small or static dataset (e.g. a set of preference strings from 10 available). Use EntityTrait to handle individual items, such as viewing a single user page, or when it is unlikely you will use relations for most of the items.


All versions of relations with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4 || ^8.0
tatter/schemas Version ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package tatter/relations contains the following files

Loading the files please wait ....