PHPackages                             yii2tech/filedb - 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. yii2tech/filedb

AbandonedArchivedYii2-extension[Database &amp; ORM](/categories/database)

yii2tech/filedb
===============

Provides ActiveRecord interface for data declared in static files

1.0.6(8y ago)7186.8k↓23.5%1310BSD-3-ClausePHP

Since Dec 26Pushed 6y ago12 watchersCompare

[ Source](https://github.com/yii2tech/filedb)[ Packagist](https://packagist.org/packages/yii2tech/filedb)[ RSS](/packages/yii2tech-filedb/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (9)Used By (10)

 [ ![](https://avatars2.githubusercontent.com/u/12951949) ](https://github.com/yii2tech)

File DB Extension for Yii 2
===========================

[](#file-db-extension-for-yii-2)

This extension provides ActiveRecord interface for the data declared in static files. Such solution allows declaration of static entities like groups, statuses and so on via files, which are stored under version control instead of database.

> Note: although this extension allows writing of the data, it is not recommended. You should consider using regular relational database based on SQLite in case you need sophisticated local data storage.

For license information check the [LICENSE](LICENSE.md)-file.

[![Latest Stable Version](https://camo.githubusercontent.com/3e8c08d2f25e5bd6d301d1948d6ebda40358d8f6ea20d41ca0bd902642d3509f/68747470733a2f2f706f7365722e707567782e6f72672f79696932746563682f66696c6564622f762f737461626c652e706e67)](https://packagist.org/packages/yii2tech/filedb)[![Total Downloads](https://camo.githubusercontent.com/2913e4ebbc0edc549948b841247f80d2ab9fe467ce0085adb2e025a8434ed9f2/68747470733a2f2f706f7365722e707567782e6f72672f79696932746563682f66696c6564622f646f776e6c6f6164732e706e67)](https://packagist.org/packages/yii2tech/filedb)[![Build Status](https://camo.githubusercontent.com/012564c2a52612d0fb55a77ff2e470fffa7cd6c270acedd66fdcf0f6136e2fec/68747470733a2f2f7472617669732d63692e6f72672f79696932746563682f66696c6564622e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yii2tech/filedb)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist yii2tech/filedb

```

or add

```
"yii2tech/filedb": "*"
```

to the require section of your composer.json.

Usage
-----

[](#usage)

This extension works similar to regular Yii2 database access layer. First of all you should add a \[\[\\yii2tech\\filedb\\Connection\]\] component to your application configuration:

```
return [
    'components' => [
        'filedb' => [
            'class' => 'yii2tech\filedb\Connection',
            'path' => '@app/data/static',
        ],
        // ...
    ],
    // ...
];
```

Now you can declare actual entities and their data via files stored under '@app/data/static' path. By default regular PHP code files are used for this, but you can choose different format via \[\[\\yii2tech\\filedb\\Connection::format\]\]. Each entity should have a file with corresponding name, like 'UserGroup', 'ItemStatus' etc. So full file names for them would be '/path/to/project/data/static/UserGroup.php', '/path/to/project/data/static/ItemStatus.php' and so on. Each file should return an array containing actual entity data, for example:

```
// file 'UserGroup.php'
return [
    [
        'id' => 1,
        'name' => 'admin',
        'description' => 'Site administrator',
    ],
    [
        'id' => 2,
        'name' => 'member',
        'description' => 'Registered front-end user',
    ],
];
```

In file DB each data row should have a unique field, which identifies it - a primary key. Its name is specified by \[\[\\yii2tech\\filedb\\Connection::primaryKeyName\]\]. You may ommit primary key at rows declaration in this case key, under which row is declared in the data array will be used as primary key value. So previous data file example can be rewritten in following way:

```
// file 'UserGroup.php'
return [
    1 => [
        'name' => 'admin',
        'description' => 'Site administrator',
    ],
    2 => [
        'name' => 'member',
        'description' => 'Registered front-end user',
    ],
];
```

Querying Data
--------------

[](#querying-data-)

You may execute complex query on the data declared in files using \[\[\\yii2tech\\filedb\\Query\]\] class. This class works similar to regular \[\[\\yii\\db\\Query\]\] and uses same syntax. For example:

```
use yii2tech\filedb\Query;

$query = new Query();
$query->from('UserGroup')
    ->limit(10);
$rows = $query->all();

$query = new Query();
$row = $query->from('UserGroup')
    ->where(['name' => 'admin'])
    ->one();
```

Using ActiveRecord
-------------------

[](#using-activerecord-)

The main purpose of this extension is provide an ActiveRecord interface for the static data. It is done via \[\[\\yii2tech\\filedb\\ActiveRecord\]\] and \[\[\\yii2tech\\filedb\\ActiveQuery\]\] classes. Particular ActiveRecord class should extend \[\[\\yii2tech\\filedb\\ActiveRecord\]\] and override its `fileName()` method, specifying source data file name. For example:

```
class UserGroup extends \yii2tech\filedb\ActiveRecord
{
    public static function fileName()
    {
        return 'UserGroup';
    }
}
```

> Note: by default `fileName()` returns own class base name (without namespace), so if you declare source data file with name equal to the class base name, you can ommit `fileName()` method overriding.

\[\[\\yii2tech\\filedb\\ActiveRecord\]\] works similar to regular \[\[\\yii\\db\\ActiveRecord\]\], allowing finding, validation and saving models. It can establish relations to other ActiveRecord classes, which are usually represents entities from relational database. For example:

```
class UserGroup extends \yii2tech\filedb\ActiveRecord
{
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['groupId' => 'id']);
    }
}

class User extends \yii\db\ActiveRecord
{
    public function getGroup()
    {
        return $this->hasOne(UserGroup::className(), ['id' => 'groupId']);
    }
}
```

So relational queries can be performed like following:

```
$users = User::find()->with('group')->all();
foreach ($users as $user) {
    echo 'username: ' . $user->name . "\n";
    echo 'group: ' . $user->group->name . "\n\n";
}

$adminGroup = UserGroup::find()->where(['name' => 'admin'])->one();
foreach ($adminGroup->users as $user) {
    echo $user->name . "\n";
}
```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 95.5% 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 ~119 days

Recently: every ~107 days

Total

8

Last Release

2962d ago

Major Versions

1.0.6 → 2.0.x-dev2018-04-09

### Community

Maintainers

![](https://www.gravatar.com/avatar/854af1889dd7384243cff0bf0fde23463f76058b499b15c740f02e7033ec7ce6?d=identicon)[klimov-paul](/maintainers/klimov-paul)

---

Top Contributors

[![klimov-paul](https://avatars.githubusercontent.com/u/1482054?v=4)](https://github.com/klimov-paul "klimov-paul (42 commits)")[![fps01](https://avatars.githubusercontent.com/u/2114997?v=4)](https://github.com/fps01 "fps01 (1 commits)")[![jacmoe](https://avatars.githubusercontent.com/u/85424?v=4)](https://github.com/jacmoe "jacmoe (1 commits)")

---

Tags

activerecordfiledblocalstorageyiiyii2yii2-extensionstaticrecordyii2activefiledb

### Embed Badge

![Health badge](/badges/yii2tech-filedb/health.svg)

```
[![Health](https://phpackages.com/badges/yii2tech-filedb/health.svg)](https://phpackages.com/packages/yii2tech-filedb)
```

###  Alternatives

[yii2tech/illuminate

Yii2 to Laravel Migration Package

11315.1k](/packages/yii2tech-illuminate)

PHPackages © 2026

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