tithely/picomapper
Composer 安装命令:
composer require tithely/picomapper
包简介
Minimalist data mapper built on PicoDb
README 文档
README
PicoMapper is a minimalist data mapper built on PicoDb.
Features
- Built on PicoDb
- No configuration files
- License: MIT
Requirements
- PHP >= 7.3
- PDO extension
- Sqlite, Mssql, Mysql or Postgresql
Documentation
Installation
composer require tithely/picomapper
Setup
use PicoDb\Database; use PicoMapper\Mapper; use PicoMapper\Definition; $db = new Database([...]); $mapper = new Mapper($db);
Concepts
Definition
In order to map to or from the database, a mapping must be built from a definition. A definition consists of a table, a primary key (one or more columns that uniquely identifies a record), columns, children (other definitions) and special options that control things like what is considered a deleted record.
Consider a blog system with posts, comments and authors. A post has one author and many comments, a comment has one author. Assuming posts, comments and authors have their own tables, a suitable definition would look like the following:
$author = (new Definition('authors')) ->withColumns('name') ->withDeletionTimestamp('deleted'); $comment = (new Definition('comments')) ->withColumns('content') ->withOne($author, 'author', 'id', 'author_id') ->withDeletionTimestamp('deleted'); $post = (new Definition('posts')) ->withColumns('title', 'content') ->withOne($author, 'author', 'id', 'author_id') ->withMany($comment, 'comments', 'post_id') ->withDeletionTimestamp('deleted'); $mapping = $mapper->mapping($post);
The second constructor argument for Definition is an array of columns that uniquely identify a record within the table.
By default it's ['id'].
Data to be set on insert and update can be set by calling withCreationData() and withModificationData() respectively. For example if you wanted to add a date_entered and date_modified columns to the $post definition, you would enter it as follows:
$post = (new Definition('posts')) ->withColumns('title', 'content') ->withOne($author, 'author', 'id', 'author_id') ->withMany($comment, 'comments', 'post_id') ->withCreationData(['date_entered' => gmdate('Y-m-d G:i:s')]) ->withModificationData(['date_modified' => gmdate('Y-m-d G:i:s')]); ->withDeletionTimestamp('deleted');
Mapping
Mappings have the same interface as PicoDb's Table class. That is, you can chain conditions and call findOne() or
findAll() to fetch records or insert(), update(), remove() and save() to modify records. In all cases, the
definition will be used to intelligently return or accept a structured array.
In the example above you could fetch or save a post using the following structured array, and all table relationships will be followed automatically.
$post = [ 'id' => 'abc123', 'author' => [ 'id' => 'zxy321', 'name' => 'John Doe' ], 'title' => 'Data Mappers Rock', 'content' => 'They save you time', 'comments' => [ [ 'id' => 'def456', 'post_id' => 'abc123', 'author' => [ 'id' => 'zxy321', 'name' => 'John Doe' ], 'content' => 'Did you like my post?' ], [ 'id' => 'hij789', 'post_id' => 'abc123', 'author' => [ 'id' => 'klm012', 'name' => 'Jane Doe' ], 'content' => 'Nice article!' ], ] ]; $mapper->save($post); $saved = $mapper->eq('id', 'abc123')->findOne(); // $saved will be identical in structure to post
Hooks
Hooks are callbacks that can be triggered when a mapping performs the successful insert, update or removal of a record. A hook registered against a mapper will be used for all top level mappings it creates.
$mapper->registerHook('updated', function ($table, $key, $updated, $original) { printf('Table %s (ID: %s) was updated...', $table, implode(':', $key)); });
tithely/picomapper 适用场景与选型建议
tithely/picomapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.4k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tithely/picomapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tithely/picomapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 40.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-07