sota2501/cakephp-ownership
Composer 安装命令:
composer require sota2501/cakephp-ownership
包简介
Ownership plugin for CakePHP
README 文档
README
The Ownership Plugin is a plugin designed to introduce ownership functionality (distinct from user functionality). The key distinction lies in ensuring that the owner of an entity matches the owner of the associated entity. It is important to note, however, that guaranteeing this ownership match may result in an increased number of SELECT executions.
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require sota2501/cakephp-ownership
Load the plugin by adding the following statement in your project's
src/Application.php:
public function bootstrap(): void { parent::bootstrap(); $this->addPlugin('Ownership'); }
Ownership Setup
You must implement the OwnersTableInterface in the OwnersTable (ex: UsersTable). The following is an example, but it is acceptable to implement it as is.
use Ownership\Model\Table\OwnersTableInterface; class UsersTable extends Table implements OwnersTableInterface { protected $currentEntity = null; public function getCurrentEntity(): ?EntityInterface { return $this->currentEntity; } public function setCurrentEntity(?EntityInterface $entity): void { $this->currentEntity = $entity; } }
At least, you must add the OwnershipBehavior to the table that introduces ownership (it is not necessary to add it to either the owner model or the model that does not introduce ownership). For options, specify the owner model name in 'owner' and the association key name to the owner model in 'parent'.
// ex) ArticleItemsTable => Articles => ( ... => ) Users class ArticleItemsTable extends Table { public function initialize(array $config): void { // ... $this->addBehavior('Ownership.Ownership', ['owner' => 'Users', 'parent' => 'Articles']); $this->belongsTo('Articles', [ 'foreignKey' => 'article_id', ]); // ... } }
You can add OwnershipBehavior without specifying any options when not introducing ownership.
Usage
The consistency guarantee provided by this plugin is automatically performed in beforeSave event and does not provide a way to avoid this behavior.
This plugin provides a finder to get entities owned by a specific user. Additionally, it also offers a finder for entities that are not owned, even if ownership has been introduced.
$article = $articles->find('owned', ['owner_id' => 1])->first(); $article = $articles->find('nonOwned')->first();
By utilizing methods implemented in the owner model, especially by setting the logged-in user, you can get entities owned only by the currently logged-in user without specifying the owner_id. Note that at least one of them must be specified, but if both are specified, the owner_id takes precedence.
In addition to the above functionality, the OwnershipBehavior provides two functions:
$id = $articles->getOwnerId($article); $isConsistent = $articles->isOwnerConsistent($article);
The first function gets the owner's ID in the form of an array [field_name => ID]. It returns null if the entity is not owned and false if ownership has not been introduced.
The second function is used to check the consistency of the owner.
License
The Ownership Plugin is licensed under the MIT License.
sota2501/cakephp-ownership 适用场景与选型建议
sota2501/cakephp-ownership 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 620 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sota2501/cakephp-ownership 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sota2501/cakephp-ownership 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 620
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-15