jumper423/with-join
Composer 安装命令:
composer require jumper423/with-join
包简介
Package to convert Eloquent BelongsTo subqueries into one query with left join.
README 文档
README
Usage
Mark relation you want to convert into left join using ->references($relations) method or Model::includes($relations) method:
Model::with('other')->references('other')->orderBy('other.title', 'asc')->get(); # this will make one sql-query with left join of 'other' relation # result object will be the same object Model::includes('first', 'second')->where('first.title', '=', 'my title')->get(); # will be the same as Model::with('first', 'second')->references('first', 'second')->… Model extends Eloquent { $includes = ['first', 'second']; } Model::where('first.title', '=', 'my title')->get(); # result is same as Model::includes but definition is done within the model # if you use $with and $includes together it will be merged Model::with('foreign')->orderBy('field', 'asc')->get(); # this will work with default behaviour (perform 2 sql-queries)
Example
New Behaviour
StreetImage::includes('street')->first()
will perform the following sql-query:
select `street`.`<…>` as `__f__street---<…>`, `street_images`.* from `street_images` left join `streets` as `street` on `street`.`id` = `street_images`.`street_id` order by `sort` asc limit 1
Default Behaviour
StreetImage::with('street')->first()
will perform the following sql-queries:
select `street_images`.* from `street_images` order by `sort` asc limit 1 select `streets`.* from `streets` where `streets`.`id` in (?) order by `title` asc
Object Structure
You will get the same object if you will use includes() method. For example:
StreetImage::includes('street')->first()
will return:
object(StreetImage) {
<all street image attributes>
street: object(Street) {
<all street attributes>
}
}
Structure will be the same even if you using nested relations:
StreetImage::includes('street.district')->first();
will return:
object(StreetImage) {
<all street image attributes>
street: object(Street) {
<all street attributes>
district: object(District) {
<all district attributes>
}
}
}
Nested Relations
StreetImage::includes('street.type', 'street.district')->first();
will perform a following sql-query (<…> will be replaced with all table columns):
select `street`.`<…>` as `__f__street---<…>`, `type`.`<…>` as `__f__street---__f__type---<…>`, `district`.`<…>` as `__f__street---__f__district---<…>`, `street_images`.* from `street_images` left join `streets` as `street` on `street`.`id` = `street_images`.`street_id` left join `street_types` as `type` on `type`.`id` = `street`.`street_type_id` left join `districts` as `district` on `district`.`id` = `street`.`district_id` order by `sort` asc limit 1
instead of performing 4 sql-queries by default Eloquent behaviour:
select `street_images`.* from `street_images` order by `sort` asc limit 1 select `streets`.* from `streets` where `streets`.`id` in (?) order by `title` asc select * from `street_types` where `street_types`.`id` in (?) order by `title` asc select * from `districts` where `districts`.`id` in (?) order by `sort` asc
Installation
-
Require this package in your composer.json and run composer update (or run
composer require sleeping-owl/with-join:1.xdirectly):"sleeping-owl/with-join": "1.*" -
Use
\SleepingOwl\WithJoin\WithJoinTraittrait in every eloquent model you want to use this package features:class StreetImage extends \Eloquent { use \SleepingOwl\WithJoin\WithJoinTrait; }
-
That`s all.
Support Library
You can donate via PayPal or in BTC: 13k36pym383rEmsBSLyWfT3TxCQMN2Lekd
Copyright and License
Package was written by Sleeping Owl for the Laravel framework and is released under the MIT License. See the LICENSE file for details.
jumper423/with-join 适用场景与选型建议
jumper423/with-join 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「activerecord」 「laravel」 「join」 「eloquent」 「with」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jumper423/with-join 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jumper423/with-join 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jumper423/with-join 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
ActiveRecord for API
CalendarView widget for Yii 2 Framework.
This package introduces the join magic for eloquent models and relations.
Twig filter that joins an array using a different separator for the last element
This package introduces the join magic for eloquent models and relations.
Relational attributes support for YII2 ActiveRecord
统计信息
- 总下载量: 33
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-24