定制 yii2tech/filedb 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yii2tech/filedb

最新稳定版本:1.0.6

Composer 安装命令:

composer require yii2tech/filedb

包简介

Provides ActiveRecord interface for data declared in static files

README 文档

README

12951949

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-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

"yii2tech/filedb": "*"

to the require section of your composer.json.

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

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

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

yii2tech/filedb 适用场景与选型建议

yii2tech/filedb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 85.74k 次下载、GitHub Stars 达 71, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「static」 「active」 「record」 「yii2」 「filedb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yii2tech/filedb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yii2tech/filedb 我们能提供哪些服务?
定制开发 / 二次开发

基于 yii2tech/filedb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 85.74k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 72
  • 点击次数: 20
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 71
  • Watchers: 14
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-01-04