Skip to content

Commit ae06998

Browse files
committed
Update documentaion
1 parent 2d12285 commit ae06998

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CHANGES
22

33
## v1.2.0
4-
- :sparkles: Add insert select for inserting once feature.
4+
- :sparkles: Add insert select query for inserting a record just once.
55
- :green_heart: Improve CI.
66

77
## v1.1.1

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Easy Query
22

3-
CakePHP behavior plugin for easily some complicated queries like upsert, bulk upsert and bulk insert.
3+
CakePHP behavior plugin for easily some complicated queries.
44

55
[![Build Status](https://travis-ci.org/itosho/easy-query.svg?branch=master)](https://travis-ci.org/itosho/easy-query)
66
[![codecov](https://codecov.io/gh/itosho/easy-query/branch/master/graph/badge.svg)](https://codecov.io/gh/itosho/easy-query)
@@ -83,6 +83,62 @@ $entities = $this->Articles->newEntities($data);
8383
$this->Articles->bulkInsert($entities);
8484
```
8585

86+
### Insert Select
87+
For inserting a record just once.
88+
89+
#### case1
90+
Specify search conditions.
91+
92+
```php
93+
$this->Articles = TableRegistry::get('Articles');
94+
$this->Articles->addBehavior('Itosho/EasyQuery.Insert');
95+
96+
$data = [
97+
'title' => 'New Article?',
98+
'body' => 'New Article Body?'
99+
];
100+
$entity = $this->Articles->newEntity($data);
101+
$condition = [
102+
'title' => 'New Article?'
103+
];
104+
105+
$this->Articles->insertOnce($entities);
106+
```
107+
108+
Generated SQL is below.
109+
110+
```sql
111+
INSERT INTO articles (title, body)
112+
SELECT 'New Article?', 'New Article Body?' FROM tmp WHERE NOT EXISTS (
113+
SELECT * FROM articles WHERE title = 'New Article?'
114+
)
115+
```
116+
117+
#### case2
118+
Auto set search conditions with a inserting record.
119+
120+
```php
121+
$this->Articles = TableRegistry::get('Articles');
122+
$this->Articles->addBehavior('Itosho/EasyQuery.Insert');
123+
124+
$data = [
125+
'title' => 'New Article',
126+
'body' => 'New Article Body'
127+
];
128+
$entity = $this->Articles->newEntity($data);
129+
130+
$this->Articles->insertOnce($entities);
131+
```
132+
133+
Generated SQL is below.
134+
135+
```sql
136+
INSERT INTO articles (title, body)
137+
SELECT 'New Article', 'New Article Body' FROM tmp WHERE NOT EXISTS (
138+
SELECT * FROM articles WHERE title = 'New Article' AND body = 'New Article Body'
139+
)
140+
```
141+
86142
### Advanced
87143
Need to use `Timestamp` behavior, if you want to update `created` and `modified` fields automatically.
88144
And you can change the action manually by using `event` config like this.

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "itosho/easy-query",
3-
"description": "CakePHP behavior plugin for easily some complicated queries like upsert, bulk upsert and bulk insert.",
3+
"description": "CakePHP behavior plugin for easily some complicated queries.",
44
"type": "cakephp-plugin",
55
"keywords": [
66
"cakephp",
77
"behavior",
88
"plugin",
99
"upsert",
10-
"bulk",
11-
"insert"
10+
"bulk upsert",
11+
"bulk insert",
12+
"insert select",
13+
"insert once"
1214
],
1315
"homepage": "https://github.com/itosho/easy-query",
1416
"license": "MIT",

src/Model/Behavior/InsertBehavior.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Itosho\EasyQuery\Model\Behavior;
44

5-
use App\Model\Entity\ResourceFormatterTrait;
65
use Cake\Database\Expression\QueryExpression;
76
use Cake\Database\StatementInterface;
87
use Cake\I18n\FrozenTime;

0 commit comments

Comments
 (0)