定制 alvin0/database-json-laravel 二次开发

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

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

alvin0/database-json-laravel

Composer 安装命令:

composer require alvin0/database-json-laravel

包简介

Create and manage json databases with Laravel

README 文档

README

Database Json Laravel - php flat file database based on JSON files Library to use JSON files like a database. Functionality inspired by Eloquent

Requirements

  • PHP 7.2.5+

  • Composer

Installation

You can install the package via composer

composer require alvin0/database-json-laravel

Optional: The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:

'providers' => [
    // ...
    DatabaseJson\DataBaseJsonServiceProvider::class,
];

You should publish the config/databasejson.php config file .

Structure of table files

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

Basic Usage

I. Create Model and Migration with command line

1. Model

Create model :
php artisan databasejson:model User -m
Optional :
 -m  : Generate a migrate for the given model.
 -f : Create the class even if the model already exists
Code generate
namespace  App\DatabaseJson\Models;

use DatabaseJson\Model;  

class User extends  Model
{

}

2. Migration

2.1 Create a migration table
php artisan databasejson:migration user --table=users 
Code generate
namespace  App\DatabaseJson\Migrations;

use DatabaseJson\DatabaseJson;
use DatabaseJson\Migration;

class CreateTableUserMigrateMigrate extends  Migration
{
	/**
	* How to create table
	*
	* DatabaseJson::table('NameTable',array(
	* {field_name} => {field_type} More information about field types and usage in PHPDoc
	* ));
	*/
	/**
	* Run the migrations.
	*
	* @return  void
	*/

	public  function  up()
	{
		DatabaseJson::create('users', array(
			'name' => 'string',
			'old' => 'integer',
			'created_at' => 'string',
			'updated_at' => 'string',
		));
	}

}
Optional

The --table option may also be used to indicate the name of the table. the --update option will create migrate with method update table

2.2 Update migrate table

The update only supports adding or removing columns in the table

If you want to delete the table, use this method in the function up() :

DatabaseJson::remove('table_name');
example create migrate type update
php artisan databasejson:migration user --table="users" --update
Code generate
namespace  App\DatabaseJson\Migrations;

use DatabaseJson\DatabaseJson;
use DatabaseJson\Migration;

class CreateTableUserMigrateMigrate extends  Migration
{
	/**
	* Run the migrations.
	*
	* @return  void
	*/
	public  function  up()
	{
		DatabaseJson::table('users')->addFields([
			//'name' => 'string'
			//{field_name} => {field_type} More information about field types and usage in PHPDoc
		]);
		
		//DatabaseJson::table('users')->deleteFields([
			//'name',
			//{field_name}
		//]);
	}

}
2.3 Run Migrate
php artisan databasejson:migrate
Optional

--fresh : remove all table and up --path : Specify a path

II. Basic Usage

1. Model Conventions

By default, model expects created_at and updated_at columns to exist on your tables. If you do not wish to have these columns automatically managed by Model, set the $timestamps property on your model to false:

namespace  App\DatabaseJson\Models;

use DatabaseJson\Model;  

class User extends  Model
{
	/**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;
    
}

2. Inserting & Updating & Delete Models

2.1 Insert

To create a new record in the database, create a new model instance, set attributes on the model, then call the save or use static function create method:

use App\DatabaseJson\Models\User;

$user = new User;
$user->name = 'alvin';
$user->old = 27;
$user->save();

Example

use App\DatabaseJson\Models\User;

$user = User::create([
	'name' => 'alvin',
	'old' => 27
]);
2.2 Update

To create a new record in the database, create a new model instance, set attributes on the model, then call the save or use static function create method:

use App\DatabaseJson\Models\User;

$user = new User;
$user->id = 1;
$user->name = 'alvin';
$user->old = 27;
$user->save();

Example

use App\DatabaseJson\Models\User;

$id = 1;
$user = User::update([
	'name' => 'alvin',
	'old' => 28
],$id);
2.3 Delete

Remove data with constraints

use App\DatabaseJson\Models\User;

$id = 1;
$user = User::where('name','alvin')->delete();
$userById = User::find(1)->delete();

Remove all data in table

use App\DatabaseJson\Models\User;

$id = 1;
$user = User::delete();

3. Retrieving Models

all() -> This is a static function used to retrieve all objects in the model.
find($id) -> This is a static function used to retrieve an object by id in the model.

Adding Additional Constraints

You may add constraints to queries, and then use the get() or paginate($perpage) method to retrieve the results

where() - filter records ( Standard operators =, !=, >, <, >=, <=, like )
orWhere() - other type of filtering results.
orderBy() - sort rows by key in order, can order by more than one field (just chain it).
groupBy() - group rows by field.

Example :

  • use all() retrieve the results
use App\DatabaseJson\Models\User;

$users = User::all();
  • use get() retrieve the results
use App\DatabaseJson\Models\User;

$users = User::where('name','alvin')
   ->where('old', '>=', 18)
   ->get();
  • use paginate($perpage) retrieve the results
use App\DatabaseJson\Models\User;

$users = User::paginate(10);
  • add constraints
use App\DatabaseJson\Models\User;

$users = User::where('old', '>=', 18)->paginate(10);

4 Relations

4.1 setup relationship

There are 2 relationships when applied : belongsTo and hasMany

local_key default is id

  • belongsTo foreign_key default is primaryKey table relation
return $this->belongsTo('App\DatabaseJson\Models\User', 'local_key', 'foreign_key');
namespace  App\DatabaseJson\Models;

use DatabaseJson\Model;  

class Blog extends  Model
{
	public  function  user()
	{
		$this->belongsTo(User::class);
	}
    
}
  • hasMany
return $this->hasMany('App\DatabaseJson\Models\Blog', 'foreign_key', 'local_key');
namespace  App\DatabaseJson\Models;

use DatabaseJson\Model;  

class User extends  Model
{
	public  function  blogs()
	{
		return  $this->hasMany(Blog::class, 'user_id');
	}
    
}
4.2 Retrieve relationship
  • belongsTo
	//return model App\DatabaseJson\Models\User
	$userBlog = Blog::find(1)->user
  • hasMany
	//return Illuminate\Support\Collection
	$blogsByUser = User::find(1)->blogs
  • Appends relational data to model results when retrieved with function with()
	$users = User::with('blogs')->where('id', 1)->get();

5 creating the accessor

5.1Defining An Accessor

To define an accessor, create a getFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access. In this example, we'll define an accessor for the first_name attribute. The accessor will automatically be called by Eloquent when attempting to retrieve the value of the first_name attribute:

<?php

namespace App;

namespace  App\DatabaseJson\Models;

class User extends Model
{
    /**
     * Get the user's first name.
     *
     * @param  string  $value
     * @return string
     */
    public function getFirstNameAttribute($value)
    {
        return ucfirst($value);
    }
}

As you can see, the original value of the column is passed to the accessor, allowing you to manipulate and return the value. To access the value of the accessor, you may access the first_name attribute on a model instance:

$user = \App\DatabaseJson\Models\User::find(1);

$firstName = $user->first_name;
5.2 Appending Values

After creating the accessor, add the attribute name to the appends property on the model. Note that attribute names are typically referenced in "snake case", even though the accessor is defined using "camel case":

<?php

namespace App;

namespace  App\DatabaseJson\Models;

class User extends Model
{
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['first_name'];
}

Description

More informations you can find in PHPDoc, I think it's documented very well.

This is a development project from the Lazer-Database project :link https://github.com/Greg0/Lazer-Database

alvin0/database-json-laravel 适用场景与选型建议

alvin0/database-json-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.55k 次下载、GitHub Stars 达 34, 最近一次更新时间为 2018 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 34
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 34
  • Watchers: 1
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-16