greg0 / lazer-database
PHP库,用于将JSON文件用作平面文件数据库
2.0.2
2023-11-28 16:40 UTC
Requires
- php: ^7.1 || ^8.0
Requires (Dev)
- dms/phpunit-arraysubset-asserts: ^0.4.0
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^7.0 || ^8.0
- yoast/phpunit-polyfills: ^1.0
- dev-master
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.0
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-anant-svc-upcomming
- dev-migrate-to-github-actions
- dev-feature/php-7
- dev-master_clone
- dev-bugfix/anant-svc-mikey179-vfsstream
- dev-hotfix/type-double-validation
- dev-feature/type-array-in-set
- dev-hotfix/accept-null-data-error
- dev-special-char-issue
- dev-new-feature-to-save
- dev-build
This package is auto-updated.
Last update: 2024-08-28 18:16:36 UTC
README
PHP库,用于将JSON文件用作数据库。
功能灵感来源于ORM
要求
- PHP 7.1+
- Composer
安装
安装Lazer Database最简单的方法是使用Composer。当然,您可以使用自己的自动加载器,但您必须自己正确配置它。您可以在Packagist.org上找到我的包Packagist.org。
要将库添加到依赖项中,执行
composer require greg0/lazer-database
测试
运行单元测试的最简单方法是使用Composer脚本
composer run test
您还可以使用Docker
docker build -t lazer-db .
docker run -it --rm lazer-db
表文件结构
table_name.data.json
- 包含数据的表文件
table_name.config.json
- 包含配置的表文件
基本用法
首先,您应该定义一个常量LAZER_DATA_PATH
,包含包含JSON文件的文件夹的绝对路径
define('LAZER_DATA_PATH', realpath(__DIR__).'/data/'); //Path to folder with tables
然后设置命名空间
use Lazer\Classes\Database as Lazer; // example
方法
链式方法
setField()
- 设置字段值(替代魔术方法__set()
)limit()
- 返回一定范围内的结果。应在findAll()
方法之前使用。orderBy()
- 按顺序对行进行排序,可以按多个字段排序(只需链式连接即可)。groupBy()
- 按字段分组。where()
- 过滤记录。别名:and_where()
。orWhere()
- 其他类型的过滤结果。with()
- 通过定义的关系连接其他表
结束方法
getField
- 获取字段值(替代魔术方法__get()
)issetField
- 检查字段是否设置(替代魔术方法__isset()
)addFields()
- 将新字段追加到现有表中deleteFields()
- 从现有表中删除字段set()
- 获取键/值对数组参数以保存。save()
- 插入或更新数据(自动检测是否需要插入或更新)。insert()
- 强制插入。delete()
- 删除数据。relations()
- 返回包含表关系的数组。config()
- 返回包含配置的对象。fields()
- 返回包含字段名称的数组。schema()
- 返回包含字段名称和字段类型的关联数组field => type
。lastId()
- 返回表中的最后一个ID。find()
- 返回具有指定ID的单行。findAll()
- 返回行。asArray()
- 返回索引或关联数组:['field_name' => 'field_name']
。应在findAll()
或find()
方法之后使用。count()
- 返回行数。应在findAll()
或find()
方法之后使用。
创建数据库
Lazer::create('table_name', array( 'id' => 'integer', 'nickname' => 'string', {field_name} => {field_type} ));
有关字段类型和PHPDoc中的用法,请参阅更多信息
删除数据库
Lazer::remove('table_name');
检查数据库是否存在
try{ \Lazer\Classes\Helpers\Validate::table('table_name')->exists(); } catch(\Lazer\Classes\LazerException $e){ //Database doesn't exist }
选择
多选
$table = Lazer::table('table_name')->findAll(); foreach($table as $row) { print_r($row); }
单记录选择
$row = Lazer::table('table_name')->find(1); echo $row->id; // or $row->getField('id')
在find()
方法中输入行的ID。
您还可以执行如下操作以获取第一个匹配的记录
$row = Lazer::table('table_name')->where('name', '=', 'John')->find(); echo $row->id;
插入
$row = Lazer::table('table_name'); $row->nickname = 'new_user'; // or $row->setField('nickname', 'new_user') $row->save();
不要设置ID。
更新
它与插入
非常相似。
$row = Lazer::table('table_name')->find(1); //Edit row with ID 1 $row->nickname = 'edited_user'; // or $row->setField('nickname', 'edited_user') $row->save();
您还可以通过简单的键值数组设置许多字段
$row = Lazer::table('table_name')->find(1); //Edit row with ID 1 $row->set(array( 'nickname' => 'user', 'email' => 'user@example.com' )); $row->save();
删除
单记录删除
Lazer::table('table_name')->find(1)->delete(); //Will remove row with ID 1 // OR Lazer::table('table_name')->where('name', '=', 'John')->find()->delete(); //Will remove John from DB
多记录删除
Lazer::table('table_name')->where('nickname', '=', 'edited_user')->delete();
清空表
Lazer::table('table_name')->delete();
关系
要处理关系,请使用类Relation
use Lazer\Classes\Relation; // example
关系类型
belongsTo
- 多对一关系hasMany
- 一对多关系hasAndBelongsToMany
- 多对多关系
方法
链式方法
belongsTo()
- 设置belongsTo关系hasMany()
- 设置hasMany关系hasAndBelongsToMany()
- 设置hasAndBelongsToMany关系localKey()
- 设置关系本地键foreignKey()
- 设置关系外键with()
- 允许对现有关系进行操作
结束方法
setRelation()
- 创建指定的关系removeRelation()
- 移除关系getRelation()
- 返回关系信息getJunction()
- 返回hasAndBelongsToMany关系中连接表的名字
创建关系
Relation::table('table1')->belongsTo('table2')->localKey('table2_id')->foreignKey('id')->setRelation(); Relation::table('table2')->hasMany('table1')->localKey('id')->foreignKey('table2_id')->setRelation(); Relation::table('table2')->hasAndBelongsToMany('table1')->localKey('id')->foreignKey('id')->setRelation(); // Junction table will be crete automaticly
移除关系
Relation::table('table1')->with('table2')->removeRelation();
获取关系信息
您可以通过两种方式实现。使用标准数据库类或关系,但结果可能会有所不同。
Relation::table('table1')->with('table2')->getRelation(); // relation with specified table Lazer::table('table1')->relations(); // all relations Lazer::table('table1')->relations('table2'); // relation with specified table
描述
有关示例,请参阅示例和教程文件。更多信息您可以在PHPDoc中找到,我认为它被很好地记录了。
电子邮件:gerg0sz92@gmail.com
如果您喜欢并使用/想要使用我的仓库或您有任何建议,我将非常感激通过电子邮件给我一些反馈。