• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Relations Module

#1
Hi all! The compliment to my Schemas module, Relations provides very basic relationship loading. This is *not* a fully-featured ORM nor does it ever intend to be, but it does provide basic eager loading for related items (entity lazy loading coming with a future release).

Tatter/Relations - Entity relationships for CodeIgniter 4

Basic usage:

  1. Install with Composer: `> composer require tatter/relations`
  2. Extend the model: `class UserModel extends \Tatter\Relations\Model`
  3. Load relations: `$users = $userModel->with('groups')->findAll();`
Your models extend Tatter\Relations\Model which injects related items that you define into the returned rows. Related items can be requested by adding a $with property to your model:

PHP Code:
protected $with 'groups';
// or
protected $with = ['groups''permissions']; 

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

PHP Code:
$users $userModel->with('groups')->findAll();
foreach (
$users as $userEntity)
{
echo 
"User {$user->name} has " count($user->groups) . " groups.";
... 

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

Relations relies on the Schemas module to map the database and detect related tables and their relationship. (You may also provide explicit relationships using the Schemas File Handler). Schemas will also attempt to associate your database tables back to their models and 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:

PHP Code:
// User hasMany Widgets
$user $userModel->with('widgets')->find($userId);
echo 
"User {$user->name} has " count($user->widgets) . " widgets.";

// ... but a Widget belongsTo one User
$widget $widgetModel->with('users')->find($widgetId);
echo 
$widget->name " belongs to " $widget->user->name


Thanks for reading! I'm always glad for feedback and suggestions, feel free to leave a comment here or check out the repo at https://github.com/tattersoftware/codeig...-relations.
Reply


Messages In This Thread
Relations Module - by MGatner - 09-24-2019, 08:20 AM
RE: Relations Module - by MGatner - 11-19-2019, 02:27 PM
RE: Relations Module - by nicolasriver - 11-30-2019, 08:01 AM
RE: Relations Module - by MGatner - 12-01-2019, 05:03 AM
RE: Relations Module - by x1250 - 02-17-2020, 08:24 PM
RE: Relations Module - by Basic App - 02-18-2020, 12:18 AM
RE: Relations Module - by MGatner - 02-18-2020, 04:25 AM
RE: Relations Module - by x1250 - 02-18-2020, 11:32 AM
RE: Relations Module - by MGatner - 02-18-2020, 11:36 AM
RE: Relations Module - by Basic App - 02-18-2020, 12:37 PM
RE: Relations Module - by MGatner - 02-20-2020, 04:11 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.