maxkoder/json-active-record
Composer 安装命令:
composer require maxkoder/json-active-record
包简介
JsonActiveRecord is a class to deal with JSON files like Active Record
README 文档
README
A PHP class to deal with JSON like Active Record
Requirements
- PHP >= 7.4
Install
composer require maxkoder/json-active-record
Usage
** See test.php for examples **
Simple class
class User extends JsonActiveRecord { protected static string $primaryKey = 'user_id'; protected static string $filePath = 'users.json'; public function posts(): array { // Will return posts's user (array of Post objects) // based on author_user_id foreign key (from posts) // third parameter is local key, default to $primaryKey return $this->hasMany(Post::class, 'author_user_id'); } }
You have to create a class that extends JsonActiveRecord users.json (the path to the file) will be used to store data, it will be created automatically
Primary Key
If the primary key is different from 'id', you have to set up in the class
protected static string $primaryKey = 'user_id';
Primary key is automatically incremented if not provided :
$user = (new User(['name' => 'John Doe', 'email' => 'john@example.com']))->save(); echo $user->user_id; // 1 ### Save / Update When saving datas, if the primary key is not provided, it will be automatically generated If provided, datas will be updated : ```php $user = (new User(['name' => 'John Doe', 'email' => 'john@example.com']))->save(); $user->name = 'Johnny Boy'; $user->save(); echo $user->name; // Johnny Boy
Temporary attributes
You can also set temporary attributes that will not be saved. Attributes name start with "_" :
$user = (new User(['name' => 'John Doe', 'email' => 'john@example.com']))->save(); $user->_tempPassword = 'secret123'; $user->save(); echo $user->_tempPassword; // null
Delete
$user = (new User(['name' => 'John Doe', 'email' => 'john@example.com']))->save(); $user->delete();
Query Builder
$users = User::queryBuilder() ->where('active', '=', 1) ->get(); // return array of User objects
Get all records
$users = User::all(); // return array of User objects
Get first/last record
(new User(['name' => 'John Doe', 'email' => 'john@example.com']))->save(); (new User(['name' => 'Jane Doe', 'email' => 'jane@example.com']))->save(); (new User(['name' => 'Johnny Boy', 'email' => 'johnny@example.com']))->save(); echo User::first()->name; // John Doe echo User::last()->name; // Johnny Boy
Special methods
echo User::findPK(1)->name; // = User::find('user_id', 1)->name
maxkoder/json-active-record 适用场景与选型建议
maxkoder/json-active-record 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 maxkoder/json-active-record 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 maxkoder/json-active-record 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-10