承接 maxkoder/json-active-record 相关项目开发

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

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

maxkoder/json-active-record

最新稳定版本:v1.0.2

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-10