定制 dscmall/laravel-orm-hasin 二次开发

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

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

dscmall/laravel-orm-hasin

Composer 安装命令:

composer require dscmall/laravel-orm-hasin

包简介

Laravel framework relation has in implement

README 文档

README

hasin是一个基于where in语法实现的Laravel ORM关联关系查询的扩展包,部分业务场景下可以替代Laravel ORM中基于where exists语法实现的has,以获取更高的性能。

环境

  • PHP >= 7.1
  • laravel >= 5.8

安装

composer require dscmall/laravel-orm-hasin

简介

Laravel ORM的关联关系非常强大,基于关联关系的查询has也给我们提供了诸多灵活的调用方式,然而某些情形下,has使用了where exists语法实现

select * from A where exists (select * from B where A.id=B.a_id)

exists是对外表做loop循环,每次loop循环再对内表(子查询)进行查询,那么因为对内表的查询使用的索引(内表效率高,故可用大表),而外表有多大都需要遍历,不可避免(尽量用小表),故内表大的使用exists,可加快效率。

但是当A表数据量较大的时候,就会出现性能问题,那么这时候用where in语法将会极大的提高性能

select * from A where A.id in (select B.a_id from B)

in是把外表和内表做hash连接,先查询内表,再把内表结果与外表匹配,对外表使用索引(外表效率高,可用大表),而内表多大都需要查询,不可避免,故外表大的使用in,可加快效率。

因此在代码中使用has(hasMorph)或者hasIn(hasMorphIn)应由数据体量来决定……

<?php
/**
 * SQL:
 * 
 * select * from `product` 
 * where exists 
 *   ( 
 *      select * from `product_skus` 
 *      where `product`.`id` = `product_skus`.`p_id` 
 *      and `product_skus`.`deleted_at` is null 
 *   ) 
 * and `product`.`deleted_at` is null 
 * limit 10 offset 0
 */
$products = Product::has('skus')->paginate(10);

/**
 * SQL:
 * 
 * select * from `product` 
 * where `product`.`id` IN  
 *   ( 
 *      select `product_skus`.`p_id` from `product_skus` 
 *      and `product_skus`.`deleted_at` is null 
 *   ) 
 * and `product`.`deleted_at` is null 
 * limit 10 offset 0
 */
$products = Product::hasIn('skus')->paginate(10);

使用

在配置文件app.php添加配置,自动注册服务

<?php
    // ...
    
    'providers' => [
        // ...
        
        Illuminate\Dscmall\Hasin\HasinServiceProvider::class,// hasin扩展包引入
    ],

此扩展方法hasIn(hasMorphIn)支持Laravel ORM中的所有关联关系,入参调用及内部实现流程与框架的has(hasMorph)完全一致,可安全使用或替换

hasIn

// hasIn
Product::hasIn('skus')->get();

// orHasIn
Product::where('name', 'like', '%拌饭酱%')->orHasIn('skus')->get();

// doesntHaveIn
Product::doesntHaveIn('skus')->get();

// orDoesntHaveIn
Product::where('name', 'like', '%拌饭酱%')->orDoesntHaveIn('skus')->get();

whereHasIn

// whereHasIn
Product::whereHasIn('skus', function ($query) {
    $query->where('sales', '>', 10);
})->get();

// orWhereHasIn
Product::where('name', 'like', '%拌饭酱%')->orWhereHasIn('skus', function ($query) {
    $query->where('sales', '>', 10);
})->get();

// whereDoesntHaveIn
Product::whereDoesntHaveIn('skus', function ($query) {
    $query->where('sales', '>', 10);
})->get();

// orWhereDoesntHaveIn
Product::where('name', 'like', '%拌饭酱%')->orWhereDoesntHaveIn('skus', function ($query) {
    $query->where('sales', '>', 10);
})->get();

hasMorphIn

Image::hasMorphIn('imageable', [Product::class, Brand::class])->get();

嵌套关联

Product::hasIn('attrs.values')->get();

自关联

Category::hasIn('children')->get();

协议

MIT 协议

dscmall/laravel-orm-hasin 适用场景与选型建议

dscmall/laravel-orm-hasin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.2k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-26