Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/itosho/easy-query
CakePHP behavior plugin for easily generating some complicated queries like (bulk) insert/upsert etc.
https://github.com/itosho/easy-query
behavior bulk-insert cakephp cakephp-orm cakephp-plugin cakephp5 composer-package php8 upsert
Last synced: 3 months ago
JSON representation
CakePHP behavior plugin for easily generating some complicated queries like (bulk) insert/upsert etc.
- Host: GitHub
- URL: https://github.com/itosho/easy-query
- Owner: itosho
- License: mit
- Created: 2017-09-02T15:28:21.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-03-07T14:28:59.000Z (11 months ago)
- Last Synced: 2024-10-01T00:52:28.956Z (4 months ago)
- Topics: behavior, bulk-insert, cakephp, cakephp-orm, cakephp-plugin, cakephp5, composer-package, php8, upsert
- Language: PHP
- Homepage:
- Size: 123 KB
- Stars: 26
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-cakephp - Itosho/EasyQuery plugin - Behavior for easily generating some complicated queries like (bulk) insert/upsert etc. (ORM / Database / Datamapping)
README
# Easy Query
CakePHP behavior plugin for easily generating some complicated queries like (bulk) insert/upsert etc.[![codecov](https://codecov.io/gh/itosho/easy-query/branch/master/graph/badge.svg)](https://codecov.io/gh/itosho/easy-query)
[![Latest Stable Version](https://poser.pugx.org/itosho/easy-query/v/stable)](https://packagist.org/packages/itosho/easy-query)
[![Total Downloads](https://poser.pugx.org/itosho/easy-query/downloads)](https://packagist.org/packages/itosho/easy-query)
[![License](https://poser.pugx.org/itosho/easy-query/license)](https://packagist.org/packages/itosho/easy-query)## 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
```bash
composer require itosho/easy-query
```## Usage
### Upsert
```php
$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
```php
$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
```php
$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.```php
$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.
```sql
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.```php
$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.
```sql
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.```php
// 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](http://opensource.org/licenses/MIT).