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 31% of packages

Maintenance40

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

507d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15896876?v=4)[MaxKoder](/maintainers/MaxKoder)[@MaxKoder](https://github.com/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

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M117](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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