1. Go to this page and download the library: Download daycry/relations library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$user->addGroup(3);
if ($user->hasGroups([1, 3]))
{
echo 'allowed!';
}
$user->setGroups([]);
use Daycry\Relations\Contracts\RelatableInterface;
class UserModel extends Model implements RelatableInterface { use ModelTrait; }
class User extends Entity implements RelatableInterface { use EntityTrait; }
protected function afterRelations(array &$rows, array $loadedTables): void { /* mutate or log */ }
protected function afterEntityRelations(string $table, mixed &$result, bool $keysOnly): void { /* transform */ }
public bool $collectMetrics = false; // métricas de carga
public bool $cacheRelationResults = false; // cache en memoria por request de resultados reindexados
public string $belongsToStrategy = 'join'; // reservado para futura estrategia 'in'
$config = config('Relations');
$config->cacheRelationResults = true;
$users = $userModel->with('groups')->findAll();
// segunda llamada reutiliza resultados en memoria (sin nueva query)
$again = $userModel->with('groups')->findAll();
class ServicerModel extends Model implements RelatableInterface {
use \Daycry\Relations\Traits\ModelTrait;
protected $table = 'servicers';
protected $primaryKey = 'id';
// Promote the default hasMany (servicers -> lawyers) to a hasOne
protected array $relationTypeOverrides = [
'lawyers' => 'hasOne',
];
}
$servicer = (new ServicerModel())->with('lawyers')->find(1);
// Access singular promoted relation
echo $servicer->lawyer->name;
// Property 'lawyers' will not be set since the relation is treated as singleton.
// 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;
/* Define your models */
class UserModel
{
use \Daycry\Relations\Traits\ModelTrait;
protected $table = 'users';
protected $with = 'widgets';
...
/* Then in your controller */
$groups = $groupModel->whereIn('id', $groupIds)->with('users')->findAll();
foreach ($groups as $group)
{
echo "<h1>{$group->name}</h1>";
foreach ($group->users as $user)
{
echo "{$user->name} is a {$user->role} with " . count($user->widgets) . " widgets.";
}
}