定制 greg0/lazer-database 二次开发

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

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

greg0/lazer-database

Composer 安装命令:

composer require greg0/lazer-database

包简介

PHP library to use JSON files like flat-file database

README 文档

README

Unit Current Version PHP Version Downloads

PHP Library to use JSON files like a database. Functionality inspired by ORM's

Requirements

  • PHP 7.2+
  • Composer

Installation

Easiest way to install Lazer Database is to use Composer. Of course you can use your own autoloader but you must configure it properly by yourself. You can find my Package on Packagist.org.

To add library to your dependencies, execute:

composer require greg0/lazer-database

Tests

Easiest way to run unit tests is to use composer script

composer run test

You can also use docker

docker build -t lazer-db .
docker run -it --rm lazer-db

Structure of table files

table_name.data.json - table file with data table_name.config.json - table file with configuration

Basic Usage

First of all you should define constant LAZER_DATA_PATH containing absolute path to folder with JSON files:

define('LAZER_DATA_PATH', realpath(__DIR__).'/data/'); //Path to folder with tables

Then set up namespace:

use Lazer\Classes\Database as Lazer; // example

Methods

Chain methods
  • setField() - set value of field (alternative to magic __set())
  • limit() - returns results between a certain number range. Should be used right before ending method findAll().
  • orderBy() - sort rows by key in order, can order by more than one field (just chain it).
  • groupBy() - group rows by field.
  • where() - filter records. Alias: and_where().
  • orWhere() - other type of filtering results.
  • with() - join other tables by defined relations
Ending methods
  • getField - get value of field (alternative to magic __get())
  • issetField - check if field is isset (alternative to magic __isset())
  • addFields() - append new fields into existing table
  • deleteFields() - removing fields from existing table
  • set() - get key/value pair array argument to save.
  • save() - insert or Update data (automatically detect if it needs an insert or update).
  • insert() - force an insert.
  • delete() - deleting data.
  • relations() - returns array with table relations
  • config() - returns object with configuration.
  • fields() - returns array with fields name.
  • schema() - returns assoc array with fields name and fields type field => type.
  • lastId() - returns last ID from table.
  • find() - returns one row with specified ID.
  • findAll() - returns rows.
  • asArray() - returns data as indexed or assoc array: ['field_name' => 'field_name']. Should be used after ending method findAll() or find().
  • count() - returns the number of rows. Should be used after ending method findAll() or find().

Create database

Lazer::create('table_name', array(
    'id' => 'integer',
    'nickname' => 'string',
    {field_name} => {field_type}
));

More information about field types and usage in PHPDoc

Remove database

Lazer::remove('table_name');

Check if a database exists

try{
    \Lazer\Classes\Helpers\Validate::table('table_name')->exists();
} catch(\Lazer\Classes\LazerException $e){
    //Database doesn't exist
}

Select

Multiple select

$table = Lazer::table('table_name')->findAll();

foreach($table as $row)
{
    print_r($row);
}

Single record select

$row = Lazer::table('table_name')->find(1);

echo $row->id; // or $row->getField('id')

Type ID of row in find() method.

You also can do something like that to get first matching record:

$row = Lazer::table('table_name')->where('name', '=', 'John')->find();

echo $row->id;

Insert

$row = Lazer::table('table_name');

$row->nickname = 'new_user'; // or $row->setField('nickname', 'new_user')
$row->save();

Do not set the ID.

Update

It's very smilar to Inserting.

$row = Lazer::table('table_name')->find(1); //Edit row with ID 1

$row->nickname = 'edited_user'; // or $row->setField('nickname', 'edited_user')
$row->save();

You can also set many field by simple key-value array

$row = Lazer::table('table_name')->find(1); //Edit row with ID 1

$row->set(array(
    'nickname' => 'user',
    'email' => 'user@example.com'
));
$row->save();

Remove

Single record deleting

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

Multiple records deleting

Lazer::table('table_name')->where('nickname', '=', 'edited_user')->delete();

Clear table

Lazer::table('table_name')->delete();

Relations

To work with relations use class Relation

use Lazer\Classes\Relation; // example

Relation types

  • belongsTo - relation many to one
  • hasMany - relation one to many
  • hasAndBelongsToMany - relation many to many

Methods

Chain methods
  • belongsTo() - set relation belongsTo
  • hasMany() - set relation hasMany
  • hasAndBelongsToMany() - set relation hasAndBelongsToMany
  • localKey() - set relation local key
  • foreignKey() - set relation foreign key
  • with() - allow to work on existing relation
Ending methods
  • setRelation() - creating specified relation
  • removeRelation() - remove relation
  • getRelation() - return informations about relation
  • getJunction() - return name of junction table in hasAndBelongsToMany relation

Create relation

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

Remove relation

Relation::table('table1')->with('table2')->removeRelation();

Get relation information

You can do it by two ways. Use Standard Database class or Relation but results will be different.

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

Description

For some examples please check Examples and Tutorial file. More informations you can find in PHPDoc, I think it's documented very well.

E-mail: gerg0sz92@gmail.com

If you like and using/want to use my repo or you have any suggestions I will be greatful for sending me few words on e-mail.

greg0/lazer-database 适用场景与选型建议

greg0/lazer-database 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.51k 次下载、GitHub Stars 达 283, 最近一次更新时间为 2014 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 greg0/lazer-database 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 16.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 288
  • 点击次数: 11
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 283
  • Watchers: 11
  • Forks: 38
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-15