minionfactory/model-mapper
Composer 安装命令:
composer require minionfactory/model-mapper
包简介
Laravel extension. Wrapper for models and raw DB::select results to map one or more attributes to other models.
README 文档
README
Introduction
Model Mapper is a minor change on how models are used. Many of my real world examples are contain multiple complex joins with equally complex where clauses. Using true eloquent objects for these scenarios are not beneficial. Maintenance and readability suffer, when compared to the raw equivalent SQL query that would be used.
Other times where the join is simple enough, the attribute methods are missing except for the main class that is being instantiated.
Finally, as I do most of my work with sqlsrv (Microsoft SQL Server), it has issues with dates.
This package is meant for my own usage, but is free for others who find it useful.
Model Mapping: AdvancedModel
Using the model for simple updates / inserts etc is great. When the queries involve multiple joins with multiple conditions and subqueries, the model request starts to become messy. I've created two classes to handle this problem.
The first is the AdvancedModel. This overrides the default __get method as well as adds modelMapper($array).
Usage:
$reminderText = CommonData::join('Events', 'CommonData.CommonDataID', '=', 'Events.EventsCommonID') ->whereIn('EventsId', $ids) ->select('CommonData.reminderemailText', 'Events.EventDate', 'Events.PriceCount') ->get(); $reminderText->map(function($reminder) { $reminder->modelMapper([ 'EventDate' => Events::class, 'PriceCount' => 'integer' ]); });
When referencing $reminderText[ $index ]→EventDate, it will now use the Events class to determine how to deal with the attribute instead of going to CommonData.
For each field that's being returned that is part of another model, simply add it as a key→value pair in the modelMapper mapping call.
If the attribute requires no extra altering apart from casting it to the type that it needs to be, you can use the following simple values instead of the model class:
- integer, int
- boolean, bool
- float
- double
- string, text
Model Mapping: AdvancedResult
The second class is AdvancedResult. This is used for raw queries as DB::select(...) returns a collection. Each of the attributes can be mapped to a model. If the third parameter is supplied as true, it returns a single result. This is the object and not a collection.
$asdf = DB::select("SELECT TOP 3 * FROM Events INNER JOIN CommonData ON ..."); // The first argument is passed by reference so there's no need to set the variable to be returned. // The second argument is optional, if you want to set it later. AdvancedResult::make($asdf, [ 'EventDate' => Events::class, 'reminderemailText' => CommonData::class, 'PriceCount' => 'integer' ]); // This is how you would call the argument later. $asdf->map(function($record){ $record->modelMapper([ 'EventDate' => Events::class, 'PriceCount' => 'integer' ]); return $record; });
minionfactory/model-mapper 适用场景与选型建议
minionfactory/model-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.02k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「mapper」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 minionfactory/model-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 minionfactory/model-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 minionfactory/model-mapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
data-mapper php library
Convention-based Alternative ORM
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Laravel 5 - Repositories to the database layer
Mapper for accessing resources in KWCMS
统计信息
- 总下载量: 26.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-23