angelxmoreno/cakephp-linked-entities

CakePHP 3.x 的 LinkedEntities 插件

v1.0.3 2018-09-04 11:10 UTC

This package is auto-updated.

Last update: 2024-09-16 03:26:22 UTC


README

Build Status Codacy Badge Maintainability Test Coverage License Minimum CakePHP Version Minimum PHP Version

LinkedEntities 允许您通过分类类型将用户实体与其他实体(包括自我引用)关联起来。它为您的用户表添加了快捷函数,以便于使用。

功能

  • 能够通过多态表定义用户到实体的关系
  • 通过分类定义关系
  • 自动建立实体到用户的关系
  • 基于关系名称的快捷方法

示例

$this->Users->addStarredProject($user, $project);
$this->Users->removeFollowedUser($user, $otherUser);

要求

  • CakePHP 3.x
  • PHP >=5.6

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方法是

composer require angelxmoreno/cakephp-linked-entities

接下来,您需要通过向您的 config/bootstrap.php 文件中添加以下内容来加载插件

// config/bootstrap.php
Plugin::load('LinkedEntities', ['bootstrap' => true]);

最后,使用 cakephp/migrations 创建所需的表。

bin/cake migrations migrate -p LinkedEntities

或者导入 config/schema 中找到的 sql 架构。

设置

  1. 在您的 config/app.php 中定义一个名为 LinkedEntities 的新配置键(见配置
  2. 在您的 UsersTable 中添加 LinkedEntities.LinkableEntityUser 行为,如下所示
$this->addBehavior('LinkedEntities.LinkableEntityUser');
  1. 可选地,将 LinkedEntities.LinkableEntity 行为添加到您的配置中定义的相应 Table 类中。例如
$this->addBehavior('LinkedEntities.LinkableEntity');

配置

示例配置

// config/app.php
'LinkedEntities' => [
    'UserModel' => 'Users',
    'link_types' => [
        'star' => 1,
        'follow' => 2,
    ],
    'links' => [
        'StarredProjects' => [
            'name' => 'UserStars',
            'className' => 'Projects',
            'linkType' => 1
        ],
        'FollowedProjects' => [
            'name' => 'Followers',
            'className' => 'Projects',
            'linkType' => 2
        ],
        'FollowedUsers' => [
            'name' => 'Followers',
            'className' => 'Users',
            'linkType' => 2
        ]
    ]
]

使用上述配置(添加行为后),您将在 UsersTable 中获得 6 个新方法

  1. $this->Users->addStarredProjects($user, $project);
  2. $this->Users->removeStarredProjects($user, $project);
  3. $this->Users->addFollowedProjects($user, $project);
  4. $this->Users->removeFollowedProjects($user, $project);
  5. $this->Users->addFollowedUsers($user, $otherUser);
  6. $this->Users->removeFollowedUsers($user, otherUser);

UserModel 参数

一个定义您的 UsersTable 插件.name 的字符串(默认为 Users

link_types

一个 int => string 的数组(这保存了在表列 type 下提供的 int)

links

一个 relationship name => settings 的数组,设置具有以下键

  • name: 从关联实体的角度看的关联名称(反向关系名称)
  • className: 关联用户到的表名称
  • linkType: 与定义的 link_types 之一对应的 int 值

报告问题

如果您遇到插件问题,请在 GitHub 上打开一个问题。

许可证

此插件在 MIT 许可证 下提供。