itosho/easy-query
Composer 安装命令:
composer require itosho/easy-query
包简介
CakePHP behavior plugin for easily generating some complicated queries like (bulk) insert/upsert etc.
README 文档
README
CakePHP behavior plugin for easily generating some complicated queries like (bulk) insert/upsert etc.
Requirements
- PHP 8.1+
- CakePHP 5.0+
- MySQL 8.0+ / MariaDB 10.4+
Notice
- For CakePHP4.x, use 3.x tag.
- For CakePHP3.x, use 1.x tag.
Installation
composer require itosho/easy-query
Usage
Upsert
$this->Tags = TableRegistry::getTableLocator()->get('Tags'); $this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [ 'uniqueColumns' => ['name'], 'updateColumns' => ['description', 'modified'], ]); $data = [ 'name' => 'cakephp', 'description' => 'php web framework', ]; $entity = $this->Tags->newEntity($data); $this->Tags->upsert($entity);
Bulk Upsert
$this->Tags = TableRegistry::getTableLocator()->get('Tags'); $this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [ 'updateColumns' => ['description', 'modified'], ]); $data = [ [ 'name' => 'cakephp', 'description' => 'php web framework', ], [ 'name' => 'rubyonrails', 'description' => 'ruby web framework', ] ]; $entities = $this->Tags->newEntities($data); $this->Tags->bulkUpsert($entities);
Bulk Insert
$this->Articles = TableRegistry::getTableLocator()->get('Articles'); $this->Articles->addBehavior('Itosho/EasyQuery.Insert'); $data = [ [ 'title' => 'First Article', 'body' => 'First Article Body', 'published' => '1', ], [ 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => '0', ] ]; $entities = $this->Articles->newEntities($data); $this->Articles->bulkInsert($entities);
Insert Select
For inserting a record just once.
case1
Specify search conditions.
$this->Articles = TableRegistry::getTableLocator()->get('Articles'); $this->Articles->addBehavior('Itosho/EasyQuery.Insert'); $data = [ 'title' => 'New Article?', 'body' => 'New Article Body?', ]; $entity = $this->Articles->newEntity($data); $condition = ['title' => 'New Article?']; $this->Articles->insertOnce($entities);
Generated SQL is below.
INSERT INTO articles (title, body) SELECT 'New Article?', 'New Article Body?' FROM tmp WHERE NOT EXISTS ( SELECT * FROM articles WHERE title = 'New Article?' )
case2
Auto set search conditions with a inserting record.
$this->Articles = TableRegistry::getTableLocator()->get('Articles'); $this->Articles->addBehavior('Itosho/EasyQuery.Insert'); $data = [ 'title' => 'New Article', 'body' => 'New Article Body', ]; $entity = $this->Articles->newEntity($data); $this->Articles->insertOnce($entities);
Generated SQL is below.
INSERT INTO articles (title, body) SELECT 'New Article', 'New Article Body' FROM tmp WHERE NOT EXISTS ( SELECT * FROM articles WHERE title = 'New Article' AND body = 'New Article Body' )
Advanced
Need to use Timestamp behavior, if you want to update created and modified fields automatically.
And you can change the action manually by using event config like this.
// default value is true $this->Articles->addBehavior('Itosho/EasyQuery.Insert', [ 'event' => ['beforeSave' => false], ]);
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/itosho/easy-query.
License
The plugin is available as open source under the terms of the MIT License.
itosho/easy-query 适用场景与选型建议
itosho/easy-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.37k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2017 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「plugin」 「cakephp」 「Behavior」 「bulk insert」 「upsert」 「bulk upsert」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 itosho/easy-query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 itosho/easy-query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 itosho/easy-query 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
A CakePHP plugin to provide easy access to zumba/swivel
This is a set of Yii2 behaviors extensions
Email Toolkit Plugin for CakePHP
Rhino, a CMS and Application-Framework plugin for CakePHP
A CakePHP plugin for sending mail via SparkPost's REST API
统计信息
- 总下载量: 24.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-08