{"id":13507432,"url":"https://github.com/yii2tech/filedb","last_synced_at":"2025-10-01T02:31:14.754Z","repository":{"id":57087033,"uuid":"38607539","full_name":"yii2tech/filedb","owner":"yii2tech","description":"ActiveRecord for static data definitions based on files","archived":true,"fork":false,"pushed_at":"2019-07-02T11:37:49.000Z","size":59,"stargazers_count":73,"open_issues_count":0,"forks_count":12,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-09-22T22:01:57.261Z","etag":null,"topics":["activerecord","filedb","localstorage","yii","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yii2tech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["klimov-paul"],"patreon":"klimov_paul"}},"created_at":"2015-07-06T08:46:40.000Z","updated_at":"2023-10-08T00:00:49.000Z","dependencies_parsed_at":"2022-08-20T15:31:14.105Z","dependency_job_id":null,"html_url":"https://github.com/yii2tech/filedb","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Ffiledb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Ffiledb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Ffiledb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Ffiledb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yii2tech","download_url":"https://codeload.github.com/yii2tech/filedb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219875269,"owners_count":16554660,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","filedb","localstorage","yii","yii2","yii2-extension"],"created_at":"2024-08-01T02:00:33.441Z","updated_at":"2025-10-01T02:31:14.444Z","avatar_url":"https://github.com/yii2tech.png","language":"PHP","readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yii2tech\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://avatars2.githubusercontent.com/u/12951949\" height=\"100px\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eFile DB Extension for Yii 2\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nThis extension provides ActiveRecord interface for the data declared in static files.\nSuch solution allows declaration of static entities like groups, statuses and so on via files, which are\nstored under version control instead of database.\n\n\u003e Note: although this extension allows writing of the data, it is not recommended. You should consider\n  using regular relational database based on SQLite in case you need sophisticated local data storage.\n\nFor license information check the [LICENSE](LICENSE.md)-file.\n\n[![Latest Stable Version](https://poser.pugx.org/yii2tech/filedb/v/stable.png)](https://packagist.org/packages/yii2tech/filedb)\n[![Total Downloads](https://poser.pugx.org/yii2tech/filedb/downloads.png)](https://packagist.org/packages/yii2tech/filedb)\n[![Build Status](https://travis-ci.org/yii2tech/filedb.svg?branch=master)](https://travis-ci.org/yii2tech/filedb)\n\n\nInstallation\n------------\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist yii2tech/filedb\n```\n\nor add\n\n```json\n\"yii2tech/filedb\": \"*\"\n```\n\nto the require section of your composer.json.\n\n\nUsage\n-----\n\nThis extension works similar to regular Yii2 database access layer.\nFirst of all you should add a [[\\yii2tech\\filedb\\Connection]] component to your application configuration:\n\n```php\nreturn [\n    'components' =\u003e [\n        'filedb' =\u003e [\n            'class' =\u003e 'yii2tech\\filedb\\Connection',\n            'path' =\u003e '@app/data/static',\n        ],\n        // ...\n    ],\n    // ...\n];\n```\n\nNow you can declare actual entities and their data via files stored under '@app/data/static' path.\nBy default regular PHP code files are used for this, but you can choose different format via [[\\yii2tech\\filedb\\Connection::format]].\nEach entity should have a file with corresponding name, like 'UserGroup', 'ItemStatus' etc. So full file names for\nthem would be '/path/to/project/data/static/UserGroup.php', '/path/to/project/data/static/ItemStatus.php' and so on.\nEach file should return an array containing actual entity data, for example:\n\n```php\n// file 'UserGroup.php'\nreturn [\n    [\n        'id' =\u003e 1,\n        'name' =\u003e 'admin',\n        'description' =\u003e 'Site administrator',\n    ],\n    [\n        'id' =\u003e 2,\n        'name' =\u003e 'member',\n        'description' =\u003e 'Registered front-end user',\n    ],\n];\n```\n\nIn file DB each data row should have a unique field, which identifies it - a primary key. Its name is specified\nby [[\\yii2tech\\filedb\\Connection::primaryKeyName]].\nYou may ommit primary key at rows declaration in this case key, under which row is declared in the data array will\nbe used as primary key value. So previous data file example can be rewritten in following way:\n\n```php\n// file 'UserGroup.php'\nreturn [\n    1 =\u003e [\n        'name' =\u003e 'admin',\n        'description' =\u003e 'Site administrator',\n    ],\n    2 =\u003e [\n        'name' =\u003e 'member',\n        'description' =\u003e 'Registered front-end user',\n    ],\n];\n```\n\n\n## Querying Data \u003cspan id=\"querying-data\"\u003e\u003c/span\u003e\n\nYou may execute complex query on the data declared in files using [[\\yii2tech\\filedb\\Query]] class.\nThis class works similar to regular [[\\yii\\db\\Query]] and uses same syntax.\nFor example:\n\n```php\nuse yii2tech\\filedb\\Query;\n\n$query = new Query();\n$query-\u003efrom('UserGroup')\n    -\u003elimit(10);\n$rows = $query-\u003eall();\n\n$query = new Query();\n$row = $query-\u003efrom('UserGroup')\n    -\u003ewhere(['name' =\u003e 'admin'])\n    -\u003eone();\n```\n\n\n## Using ActiveRecord \u003cspan id=\"using-active-record\"\u003e\u003c/span\u003e\n\nThe main purpose of this extension is provide an ActiveRecord interface for the static data.\nIt is done via [[\\yii2tech\\filedb\\ActiveRecord]] and [[\\yii2tech\\filedb\\ActiveQuery]] classes.\nParticular ActiveRecord class should extend [[\\yii2tech\\filedb\\ActiveRecord]] and override its `fileName()` method,\nspecifying source data file name. For example:\n\n```php\nclass UserGroup extends \\yii2tech\\filedb\\ActiveRecord\n{\n    public static function fileName()\n    {\n        return 'UserGroup';\n    }\n}\n```\n\n\u003e Note: by default `fileName()` returns own class base name (without namespace), so if you declare source data\n  file with name equal to the class base name, you can ommit `fileName()` method overriding.\n\n[[\\yii2tech\\filedb\\ActiveRecord]] works similar to regular [[\\yii\\db\\ActiveRecord]], allowing finding, validation\nand saving models. It can establish relations to other ActiveRecord classes, which are usually represents entities\nfrom relational database. For example:\n\n```php\nclass UserGroup extends \\yii2tech\\filedb\\ActiveRecord\n{\n    public function getUsers()\n    {\n        return $this-\u003ehasMany(User::className(), ['groupId' =\u003e 'id']);\n    }\n}\n\nclass User extends \\yii\\db\\ActiveRecord\n{\n    public function getGroup()\n    {\n        return $this-\u003ehasOne(UserGroup::className(), ['id' =\u003e 'groupId']);\n    }\n}\n```\n\nSo relational queries can be performed like following:\n\n```php\n$users = User::find()-\u003ewith('group')-\u003eall();\nforeach ($users as $user) {\n    echo 'username: ' . $user-\u003ename . \"\\n\";\n    echo 'group: ' . $user-\u003egroup-\u003ename . \"\\n\\n\";\n}\n\n$adminGroup = UserGroup::find()-\u003ewhere(['name' =\u003e 'admin'])-\u003eone();\nforeach ($adminGroup-\u003eusers as $user) {\n    echo $user-\u003ename . \"\\n\";\n}\n```\n","funding_links":["https://github.com/sponsors/klimov-paul","https://patreon.com/klimov_paul"],"categories":["Core components"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Ffiledb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyii2tech%2Ffiledb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Ffiledb/lists"}