phitech/entities
Composer 安装命令:
composer require phitech/entities
包简介
Provides methods for managing entities that consist two database tables: main and meta. It allows for an indiscrete set of attributes by storing them as key-value sets in the meta table, while the most important attributes and indexes are in the main table. This structure is inspired by the post/pos
README 文档
README
Author: Phitech Consulting
Package name: phitech/entities
Description: Provides methods for managing entities that consist two database tables: main and meta. It allows for an indiscrete set of attributes by storing them as key-value sets in the meta table, while the most important attributes and indexes are in the main table. This structure is inspired by the post/postmeta structure in Wordpress databases.
Important: This package requires MySQL DBMS. MariaDB won't do. The reason: 👇
"All databases except SQL Server require the columns in the second argument of the upsert method to have a "primary" or "unique" index. In addition, the MySQL database driver ignores the second argument of the upsert method and always uses the "primary" and "unique" indexes of the table to detect existing records." See also: https://laravel.com/docs/9.x/queries#upserts.
Installation
This library is installed using composer.
$ composer require phitech/entities
$ composer install
$ php artisan migrate
Usage
To test if everything is installed correctly in your Laravel application, run:
$ php artisan entities:test
Create a new entity:
$ php artisan entities:make <entity name single> <entity name plural>
For instance 'order':
$ php artisan entities:make order orders
An entity definition will now automatically be placed in the entities table which looks like:
{
"entity_name":"order",
"main_db_table":"orders",
"meta_db_table":"orders_meta",
"meta_instance_id_column":"order_id",
"main_required_columns":["order_id"]
}
You should now make two tables by using database migrations. You must do this manually, because this application is not able to do this for you. Examples of the two database migration scripts are added below.
Main table
$ php artisan migrate create_orders_table
Paste this script below into the newly created migration script. Edit the table name (here: orders). Add table attributes to your own preference and using the guidelines as described below.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrentOnUpdate()->nullable();
/**
* Here, define the main attributes of this entity. These should be the attributes that
* are used as (composite) keys, indexes, foreign keys, or other attributes that are so
* important that they should not be placed in the meta table.
*/
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
};
Meta table
$ php artisan migrate create_orders_meta_table
You should then paste this script below and only edit the table name (here: orders_meta), entity_id (here: order_id) parameters. Nothing else.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders_meta', function (Blueprint $table) {
$table->id();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrentOnUpdate()->nullable();
$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onUpdate('cascade')->onDelete('cascade');
$table->string('meta_key', 255)->nullable();
$table->longText('meta_value')->nullable();
$table->unique(['order_id', 'meta_key']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders_meta');
}
};
Getting started
class Order extends Entity
{
public function __construct(id = null) {
parent::__construct('order', $id);
}
}
phitech/entities 适用场景与选型建议
phitech/entities 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 10 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 phitech/entities 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phitech/entities 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 46
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-09