PHPackages                             maxkoder/json-active-record - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. maxkoder/json-active-record

ActiveLibrary[Database &amp; ORM](/categories/database)

maxkoder/json-active-record
===========================

JsonActiveRecord is a class to deal with JSON files like Active Record

v1.0.2(1y ago)04MITPHP

Since Feb 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/MaxenceCauderlier/JsonActiveRecord)[ Packagist](https://packagist.org/packages/maxkoder/json-active-record)[ RSS](/packages/maxkoder-json-active-record/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)DependenciesVersions (4)Used By (0)

JsonActiveRecord
================

[](#jsonactiverecord)

A PHP class to deal with JSON like Active Record

Requirements
------------

[](#requirements)

- PHP &gt;= 7.4

Install
-------

[](#install)

```
composer require maxkoder/json-active-record
```

Usage
-----

[](#usage)

\*\* See test.php for examples \*\*

### Simple class

[](#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

[](#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

[](#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

[](#delete)

```
$user = (new User(['name' => 'John Doe', 'email' => 'john@example.com']))->save();
$user->delete();
```

### Query Builder

[](#query-builder)

```
$users = User::queryBuilder()
    ->where('active', '=', 1)
    ->get();
// return array of User objects
```

### Get all records

[](#get-all-records)

```
$users = User::all();
// return array of User objects
```

### Get first/last record

[](#get-firstlast-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

[](#special-methods)

```
echo User::findPK(1)->name; // = User::find('user_id', 1)->name
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance42

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

462d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/eac69e06e140881875875761278b7a0f7f31d52140f21b66766028e360381261?d=identicon)[MaxKoder](/maintainers/MaxKoder)

---

Top Contributors

[![MaxenceCauderlier](https://avatars.githubusercontent.com/u/2272382?v=4)](https://github.com/MaxenceCauderlier "MaxenceCauderlier (6 commits)")

### Embed Badge

![Health badge](/badges/maxkoder-json-active-record/health.svg)

```
[![Health](https://phpackages.com/badges/maxkoder-json-active-record/health.svg)](https://phpackages.com/packages/maxkoder-json-active-record)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
