2017 © Pedro Peláez
 

yii2-extension filedb

Provides ActiveRecord interface for data declared in static files

image

yii2tech/filedb

Provides ActiveRecord interface for data declared in static files

  • Monday, April 9, 2018
  • by klimov-paul
  • Repository
  • 13 Watchers
  • 62 Stars
  • 15,063 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 8 Forks
  • 0 Open issues
  • 9 Versions
  • 10 % Grown

The README.md

, (*1)

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., (*2)

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., (*3)

For license information check the LICENSE-file., (*4)

Latest Stable Version Total Downloads Build Status, (*5)

Installation

The preferred way to install this extension is through composer., (*6)

Either run, (*7)

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

or add, (*8)

"yii2tech/filedb": "*"

to the require section of your composer.json., (*9)

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:, (*10)

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:, (*11)

// 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:, (*12)

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

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:, (*13)

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

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:, (*14)

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., (*15)

[[\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:, (*16)

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:, (*17)

$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";
}

The Versions

09/04 2018

2.0.x-dev

2.0.9999999.9999999-dev

Provides ActiveRecord interface for data declared in static files

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 static active record filedb

09/04 2018

dev-master

9999999-dev

Provides ActiveRecord interface for data declared in static files

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 static active record filedb

09/04 2018

1.0.6

1.0.6.0

Provides ActiveRecord interface for data declared in static files

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 static active record filedb

03/11 2017

1.0.5

1.0.5.0

Provides ActiveRecord interface for data declared in static files

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 static active record filedb

07/07 2017

1.0.4

1.0.4.0

Provides ActiveRecord interface for data declared in static files

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 static active record filedb

06/02 2017

1.0.3

1.0.3.0

Provides ActiveRecord interface for data declared in static files

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 static active record filedb

28/06 2016

1.0.2

1.0.2.0

Provides ActiveRecord interface for data declared in static files

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 static active record filedb

03/06 2016

1.0.1

1.0.1.0

Provides ActiveRecord interface for data declared in static files

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 static active record filedb

26/12 2015

1.0.0

1.0.0.0

Provides support for site map creation

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 static active record filedb