PHPackages                             laborra/passive-records - 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. laborra/passive-records

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

laborra/passive-records
=======================

Passive Records library for PHP

v0.1(12y ago)1321BSD-3-ClausePHP

Since Nov 4Pushed 12y ago1 watchersCompare

[ Source](https://github.com/zeeke/passive-records)[ Packagist](https://packagist.org/packages/laborra/passive-records)[ RSS](/packages/laborra-passive-records/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Passive Records for PHP
=======================

[](#passive-records-for-php)

[![Build Status](https://camo.githubusercontent.com/4e11340a054986dc0226f598eb6b9f5648c1eb99e6eb9af5dbf9318c3691a496/68747470733a2f2f7472617669732d63692e6f72672f7a65656b652f706173736976652d7265636f7264732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/zeeke/passive-records) [![Coverage Status](https://camo.githubusercontent.com/2c633f6dff3b2cb4d8dfb230ee1222fbfd84be5f18654f20fa73842cd91995db/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f7a65656b652f706173736976652d7265636f7264732f62616467652e706e67)](https://coveralls.io/r/zeeke/passive-records)

This extension aims to provide a way to model read only databases. These kind of databases are useful when a software component needs to access data that doesn't change so often. For instance, if you have to model the list of all the world countries, you can choose between two solution:

- Make a static array (or any other collection) of countries and access them with basic operations. This solution offers high performance and a good data maintainability. When data needs to change, the version control system helps you to bring the change in every environment of the application.
- Store the information in the database. In this way data is accessed using plain SQL statements or through an ORM library, providing a more productive way to build the application. When data needs to change, an SQL script (or something similar if you are using a NoSQL db) must be produced and it must run on every application environment. Advanced PHP frameworks offer tools to make this task less painful, but it is always a pleasant job.

The passive approach
--------------------

[](#the-passive-approach)

This library tries to achieve the best of the two methods described above. We would like to have a developer friendly data access interface, with good performances and a maintainable data set.

The application interface is ispired to Yii2 framework Active Record and it is based on model class that describes data structure and data contents.

Model definition
----------------

[](#model-definition)

To declare a PassiveRecord class you need to extend laborra\\db\\PassiveRecord and implement `getSchema` and `getData`methods like the following.

```
class Country extends PassiveRecord
{
    public static function getSchema ()
    {
        return array(
            'iso' => array('pk'),
            'label',
        );
    }

    public static function getData ()
    {
        return array(
            array('it', 'Italy'),
            array('us', 'United States'),
            ...
        );
    }
}

```

Accessing data
--------------

[](#accessing-data)

In the example we have a country data model that stores all countries we need. The structure is similar to relational databases: we have two columns, iso and label. The 'iso' property is the primary key of the model, so it has to be unique in the data set. The getData() function provides the data of the model. To access data we can use the ORM methods like:

```
// Find by primary key
$country = Country::find('it');
$this->assertInstanceOf('Country', $country);
$this->assertEquals('it', $country->iso);
$this->assertEquals('Italy', $country->label);

// Find by column condition
$countries = Country::find()->where('label', 'like', 'Ital%')->all();
$this->assertInstanceOf('Country', $countries[0]);

// Count by condition
$nCountries = Country::find()->where(array('label' => 'Italy'))->count();
$this->assertEquals($nCountries, 1);

```

The `getSchema` function declare the class schema and it can be expressed as array or as `PassiveSchema`. In this way we declare the properties our passive objects have, enabling read only access to them and allowing criteria search.

The `getData` function declare the content of the class passive record collection. It has to return an array matrix with the following syntax:

```
array(
    array('value 1 column 1', 'value 1 column 2', ...), // First row content
    array('value 2 column 1', 'value 2 column 2', ...), // Second row content
    ...
);

```

Each row is expressed by an array of values and the order is expected to be compliant with the return value of `getSchema`.

Adanced usage: mixing active and passive records
------------------------------------------------

[](#adanced-usage-mixing-active-and-passive-records)

Consider an application that must be secured by classic access permission: users have roles and each role can access a set of application functionalities. We have to model the user, roles and functionality data. Thus, we have a meny to meny relationship between users and roles and another between roles and functionality. Obviously, user data must be kept in a read/write database, so we will use classic active records for users and user\_role models. In this example, application roles are fixed and they cannot be modified at runtime. So, roles, functionalities and role\_functionlity models will be implemented using passive records.

```
class Role extends PassiveRecord
{
    public static function getSchema ()
    {
        return array(
            'name' => array('PK'),
            'label',
        );
    }

    public static function getData ()
    {
        return array(
            array('ADMIN', 'Administrator'),
        );
    }
}

class Functionality extends PassiveRecord
{
    public static function getSchema ()
    {
        return array(
            'id' => array('PK'),
            'label',
        );
    }

    public static function getData ()
    {
        return array(
            array('func1', 'Basic functionality'),
            array('func2', 'Other functionality'),
            array('func3', 'Admin only functionality'),
        );
    }
}

class RoleFunctionality extends PassiveRecord
{
    public static function getSchema ()
    {
        return array(
            'id' => array('PK'),
            'label',
        );
    }
}

```

Documentation
-------------

[](#documentation)

### Declaring passive record classes

[](#declaring-passive-record-classes)

\[TBD\]

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity53

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

Unknown

Total

1

Last Release

4574d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f4cc3fb52edeadd9d5c57f04ac87423306e478318cb95e917c3e8482301723f?d=identicon)[zeeke](/maintainers/zeeke)

---

Top Contributors

[![zeeke](https://avatars.githubusercontent.com/u/2817917?v=4)](https://github.com/zeeke "zeeke (25 commits)")

---

Tags

databasePassive Records

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/laborra-passive-records/health.svg)

```
[![Health](https://phpackages.com/badges/laborra-passive-records/health.svg)](https://phpackages.com/packages/laborra-passive-records)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[doctrine/orm

Object-Relational-Mapper for PHP

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

Symfony DoctrineBundle

4.8k241.3M3.3k](/packages/doctrine-doctrine-bundle)[doctrine/migrations

PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deploying changes to it. It is a very easy to use and a powerful tool.

4.8k204.8M440](/packages/doctrine-migrations)[doctrine/data-fixtures

Data Fixtures for all Doctrine Object Managers

2.9k136.1M516](/packages/doctrine-data-fixtures)[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.5k46.2M405](/packages/robmorgan-phinx)

PHPackages © 2026

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