byjg/micro-orm 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

byjg/micro-orm

Composer 安装命令:

composer require byjg/micro-orm

包简介

A micro framework for create a very simple decoupled ORM. This library intended to be very small and very simple to use

README 文档

README

sidebar_key micro-orm
tags
php databases orm

MicroOrm for PHP

A micro framework for create a very simple decoupled ORM. This library intended to be very small and very simple to use;

Sponsor Build Status Opensource ByJG GitHub source GitHub license GitHub release

Key Features:

  • Can be used with any DTO, Entity, Model or whatever class with public properties or with getter and setter
  • The repository support a variety of datasources: MySql, Sqlite, Postgres, MySQL, Oracle (see byjg/anydataset)
  • A class Mapper is used for mapping the Entity and the repository
  • Powerful mapper functions for automatic data transformation between models and database
  • Small and simple to use

Architecture

MicroORM implements Martin Fowler's enterprise patterns:

  • Repository: Mediates between domain and data mapping layers
  • Data Mapper: Separates domain objects from database tables
  • Active Record: Wraps database rows with domain logic ( alternative approach)

You can choose the pattern that best fits your application: use Repository + Data Mapper for complex domains, or Active Record for simpler CRUD-focused applications.

These are the key components:

┌──────────────────────────┐
│ Repository               │              ┌─────────────────────┐
│                          │         ┌────│        Model        │
│                          │         │    └─────────────────────┘
│          ┌───────────────┴─────┐   │               │
│          │       Mapper        │───┤               │
│          └───────────────┬─────┘   │               │
│                     │    │         │    ┌─────────────────────┐
│                     │    │         └────│    FieldMapping     │
│                     │    │              └─────────────────────┘
│                     │    │
│          ┌───────────────┴─────┐
│          │        Query        │
│          └───────────────┬─────┘
│                     │    │
│          ┌───────────────┴─────┐        ┌──────────────────────┐  
│          │  DatabaseExecutor   │────────│   DbDriverInterface  │  
│          └───────────────┬─────┘        └────────────┬─────────┘  
│                          │                           │                
└──────────────────────────┘                      .─────────.           
                                                 │           │          
                                                 │`─────────'│          
                                                 │           │          
                                                 │    DB     │          
                                                 │           │          
                                                 │           │          
                                                  `─────────'           
  • Model can be any class with public properties or with getter and setter. It is used to retrieve or save the data into the database
  • Mapper defines the relationship between the Model properties and the database fields
  • FieldMapping defines individual field mappings within the Mapper (field names, transformations, relationships via parentTable)
  • Query defines what to retrieve from/update in the database. It uses the Mapper to prepare the query to the database converting the Model properties to database fields
  • DatabaseExecutor (external package) wraps the DbDriver and provides transaction management, query execution, and access to database helpers
  • DbDriverInterface (external package) is the actual database driver implementation that connects to the database
  • Repository orchestrates all MicroORM components and uses DatabaseExecutor to interact with the database

For a detailed explanation of the architecture and when to use each layer, see Architecture Layers: Infrastructure vs Domain.

Getting Started

Table Structure

We have the following table structure in the database for this example:

CREATE TABLE `mytable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `company_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

We want to be able to interact with this table using the ORM.

Defining the Model

A Model in our context is a class that symbolizes the data you wish to store or fetch from the database. This Model class can be as simple as a class with public properties. Alternatively, it can be a class equipped with getter and setter methods for more controlled access and manipulation of the data.

To map the database fields, you can add attributes to the Model class. Each property in the Model class represents a field in the database.

Let's look at an example:

#[TableAttribute(tableName: 'mytable')]
class MyModel
{
    #[FieldAttribute(primaryKey: true)]
    public ?int $id = null;

    #[FieldAttribute()]
    public ?string $name = null;

    #[FieldAttribute(fieldName: 'company_id')
    public ?int $companyId = null;
}

In this example, we have a class MyModel with three properties: id, name, and companyId.

  • The id property is marked as a primary key.
  • The name property is a simple field.
  • The companyId property is a field with a different name in the database company_id.

The TableAttribute is used to define the table name in the database.

Connecting the repository

After defining the Model, you can connect the Model with the repository.

$dbDriver = \ByJG\AnyDataset\Db\Factory::getDbInstance('mysql://user:password@server/schema');
$repository = new \ByJG\MicroOrm\Repository($dbDriver, MyModel::class);

Querying the database

You can query the database using the repository.

$myModel = $repository->get(1);

or

$query = Query::getInstance()
    ->field('name')
    ->where('company_id = :cid', ['cid' => 1]);

$result = $repository->getByQuery($query);

or, the same example above:

$filterModel = $repository->entity([
    'company_id' => 1
]);

$query = $repository->queryInstance($filterModel);
$query->field('name');

$result = $repository->getByQuery($query);

Basics

Advanced Topics

Install

Just type:

composer require "byjg/micro-orm"

Running Tests

./vendor/bin/phpunit

Related Projects

Dependencies

flowchart TD
    byjg/micro-orm --> byjg/anydataset-db
    byjg/micro-orm --> ext-json
Loading

Open source ByJG

byjg/micro-orm 适用场景与选型建议

byjg/micro-orm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49.58k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2016 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 byjg/micro-orm 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 20
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-22