承接 antriver/laravel-registers 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

antriver/laravel-registers

Composer 安装命令:

composer require antriver/laravel-registers

包简介

A way to keep a simple list of models belonging to another model. e.g. Users that have liked a Post.

README 文档

README

This package provides 'registers', which are simple lists of models 'belonging to' another model. For example, a list of Users who have liked a Post. Or a list of Achievements a User has earnt. The registers also take care of caching what items are on the list.

Terminology

The owner of a register is the model the list belongs to. In the case of a 'post likers' register, this would be the Post model.

The object is what you add to the register / check is on the register / delete from the register. In the case of a 'post likers' register, this would be the User model.

Installation

composer require antriver/laravel-registers

Integration

Create a class that extends either AbstractBooleanRegister or AbstractValueRegister.

AbstractBooleanRegister

This type of register serves as a simple 'is it here or not' list. Example use: Users who have liked a Post.

AbstractValueRegister

This type of register allows the same setting/checking as AbstractBooleanRegister but you can also set some data. Examples use: Users who have voted on a Post. The additional data would be the user's vote.

The only difference between the two is AbstractBooleanRegister's check() method will return a boolean. But AbstractValueRegister will return whatever data you stored about the entry.

In both cases you must implement these methods:

create($object, array $data = [])

This should add an entry to the database to permanently store the action that has been performed (e.g. INSERT an entry in the post_likes or post_votes table). This should return an integer to show the result.

  • 1 = Object was added to the register.
  • 2 = Object was already on the register and so was modified.
  • 0 = Object was already on the register but the value was the same, so was not modified.

The easiest way to get these return values is to use a unique key on the 2 IDs on your database table (e.g. a unique key on postId + userId for a post likers register.) Then the query you run in create() should use some form of ON DUPLICATE KEY UPDATE and return the number of rows affected.

Hints:

The post will be accessible as $this-owner, the user will be the passed in $object.

destroy($object)

This should delete the row in the database relating to the action (e.g. DELETE the entry from the post_likes table) and return the number of rows affected. If this returns 0 an exception will be thrown, as that meant the object was not on the register.

load()

This should return all of the items on the register (e.g. SELECT all the entries about this post from the post_likes table). This method should return an associative array where the keys are the IDs of the objects and the value is information about that object's entry.

For AbstractValueRegister this is straightforward - return whatever value you stored about the entry (e.g. which way the user voted, in the case of a post voters register.)

For AbstractBooleanRegister this may make less sense, but you can return anything as the value. It's recommended to just use something like true. The reason the array is this way round is that it can be much faster to use isset() (cheking against the key) than in_array() (checking against the value) on larger arrays (see http://maettig.com/1397246220).

You can use the buildObjectsArrayFromLoadedData() helper method to provide the return value.

Examples

See ExamplePostLikesRegister.php and ExamplePostVotesRegister.php for example implementations.

Usage

Both AbstractBooleanRegister and AbstractValueRegister provide the same public methods.

add($object, array $data = [])

Add the object to the register. Ignore the $data property if using the AbstractBoolenRegister.

remove($object)

Remove the object from the register.

check($object)

For AbstractBooleanRegister, returns true or false if the object is on the register.

For AbstractValueRegister, returns the stored data if the object is on the register, otherwise returns null.

all()

Returns all the objects on the register.

keys()

Returns the primary keys of all the objects on the register.

count()

Returns the number of objects on the register.

refresh()

Updates the cache of objects on the register.

antriver/laravel-registers 适用场景与选型建议

antriver/laravel-registers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 691 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 antriver/laravel-registers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 antriver/laravel-registers 我们能提供哪些服务?
定制开发 / 二次开发

基于 antriver/laravel-registers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 691
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-11