Skip to content

Commit 1555b25

Browse files
authored
Merge pull request #29 from itosho/25-cakephp4
#25 Compatible with CakePHP4.x
2 parents 1e79901 + eedb4c7 commit 1555b25

File tree

11 files changed

+75
-86
lines changed

11 files changed

+75
-86
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ services:
44
- mysql
55

66
php:
7-
- 7.1
87
- 7.2
98
- 7.3
109
- 7.4
@@ -31,7 +30,6 @@ matrix:
3130
- php: 7.4
3231
env: PHPSTAN=1 DEFAULT=0 PREFER_LOWEST=""
3332

34-
3533
cache:
3634
directories:
3735
- vendor

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Easy Query
2-
32
CakePHP behavior plugin for easily generating some complicated queries like (bulk) insert/upsert etc.
43

54
[![Build Status](https://travis-ci.org/itosho/easy-query.svg?branch=master)](https://travis-ci.org/itosho/easy-query)
@@ -9,21 +8,20 @@ CakePHP behavior plugin for easily generating some complicated queries like (bul
98
[![License](https://poser.pugx.org/itosho/easy-query/license)](https://packagist.org/packages/itosho/easy-query)
109

1110
## Requirements
12-
13-
- PHP 7.1+
14-
- CakePHP 3.6+
11+
- PHP 7.2+
12+
- CakePHP 4.0+
1513
- MySQL 5.6+
1614

17-
## Installation
15+
:warning: For CakePHP3.x, use 1.x branch.
1816

17+
## Installation
1918
```bash
2019
composer require itosho/easy-query
2120
```
2221

2322
## Usage
2423

2524
### Upsert
26-
2725
```php
2826
$this->Tags = TableRegistry::getTableLocator()->get('Tags');
2927
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [
@@ -40,7 +38,6 @@ $this->Tags->upsert($entity);
4038
```
4139

4240
### Bulk Upsert
43-
4441
```php
4542
$this->Tags = TableRegistry::getTableLocator()->get('Tags');
4643
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [
@@ -62,7 +59,6 @@ $this->Tags->bulkUpsert($entities);
6259
```
6360

6461
### Bulk Insert
65-
6662
```php
6763
$this->Articles = TableRegistry::getTableLocator()->get('Articles');
6864
$this->Articles->addBehavior('Itosho/EasyQuery.Insert');
@@ -149,9 +145,7 @@ $this->Articles->addBehavior('Itosho/EasyQuery.Insert', [
149145
```
150146

151147
## Contributing
152-
153148
Bug reports and pull requests are welcome on GitHub at https://github.com/itosho/easy-query.
154149

155150
## License
156-
157151
The plugin is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
"source": "https://github.com/itosho/easy-query"
2727
},
2828
"require": {
29-
"php": ">=7.1.0",
30-
"cakephp/orm": "^3.6"
29+
"php": ">=7.2.0",
30+
"cakephp/orm": "^4.0"
3131
},
3232
"require-dev": {
33-
"cakephp/cakephp": "^3.6",
34-
"phpunit/phpunit": "^5.7.14|^6.0"
33+
"cakephp/cakephp": "^4.0",
34+
"phpunit/phpunit": "^8.5"
3535
},
3636
"autoload": {
3737
"psr-4": {

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
colors="true"
44
processIsolation="false"
55
stopOnFailure="false"
6-
syntaxCheck="false"
76
bootstrap="./tests/bootstrap.php"
87
>
98
<php>

src/Model/Behavior/InsertBehavior.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Itosho\EasyQuery\Model\Behavior;
45

@@ -32,15 +33,15 @@ class InsertBehavior extends Behavior
3233
* @throws LogicException no save data
3334
* @return StatementInterface query result
3435
*/
35-
public function bulkInsert(array $entities)
36+
public function bulkInsert(array $entities): StatementInterface
3637
{
3738
$saveData = [];
3839
foreach ($entities as $entity) {
3940
if ($this->_config['event']['beforeSave']) {
4041
$this->_table->dispatchEvent('Model.beforeSave', compact('entity'));
4142
}
4243
$entity->setVirtual([]);
43-
$saveData[] = $entity->toArray();
44+
array_push($saveData, $entity->toArray());
4445
}
4546

4647
if (!isset($saveData[0])) {
@@ -63,7 +64,7 @@ public function bulkInsert(array $entities)
6364
* @param array|null $conditions search conditions
6465
* @return StatementInterface query result
6566
*/
66-
public function insertOnce(Entity $entity, array $conditions = null)
67+
public function insertOnce(Entity $entity, array $conditions = null): StatementInterface
6768
{
6869
if ($this->_config['event']['beforeSave']) {
6970
$this->_table->dispatchEvent('Model.beforeSave', compact('entity'));
@@ -83,22 +84,19 @@ public function insertOnce(Entity $entity, array $conditions = null)
8384
if (is_null($existsConditions)) {
8485
$existsConditions = $this->getExistsConditions($insertData);
8586
}
86-
87-
$query = $this->_table
88-
->query()
89-
->insert($fields)
90-
->epilog(
91-
$this
92-
->buildTmpTableSelectQuery($insertData)
93-
->where(function (QueryExpression $exp) use ($existsConditions) {
94-
$query = $this->_table
95-
->find()
96-
->where($existsConditions);
97-
98-
return $exp->notExists($query);
99-
})
100-
->limit(1)
101-
);
87+
$query = $this->_table->query()->insert($fields);
88+
$subQuery = $this
89+
->buildTmpTableSelectQuery($insertData)
90+
->where(function (QueryExpression $exp) use ($existsConditions) {
91+
$query = $this->_table
92+
->find()
93+
->where($existsConditions);
94+
95+
return $exp->notExists($query);
96+
})
97+
->limit(1);
98+
/* @phpstan-ignore-next-line */
99+
$query = $query->epilog($subQuery);
102100

103101
return $query->execute();
104102
}
@@ -110,7 +108,7 @@ public function insertOnce(Entity $entity, array $conditions = null)
110108
* @throws LogicException select query is invalid
111109
* @return Query tmp table's select query
112110
*/
113-
private function buildTmpTableSelectQuery($insertData)
111+
private function buildTmpTableSelectQuery($insertData): Query
114112
{
115113
$driver = $this->_table
116114
->getConnection()
@@ -137,7 +135,6 @@ private function buildTmpTableSelectQuery($insertData)
137135
->from(
138136
sprintf('(SELECT %s) as tmp', implode(',', $schema))
139137
);
140-
/** @var Query $selectQuery */
141138
$selectQuery = $query;
142139
foreach ($binds as $key => $value) {
143140
$selectQuery->bind($key, $value);
@@ -152,7 +149,7 @@ private function buildTmpTableSelectQuery($insertData)
152149
* @param array $insertData insert data
153150
* @return array conditions
154151
*/
155-
private function getExistsConditions($insertData)
152+
private function getExistsConditions(array $insertData): array
156153
{
157154
$autoFillFields = ['created', 'modified'];
158155
$existsConditions = [];

src/Model/Behavior/UpsertBehavior.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Itosho\EasyQuery\Model\Behavior;
45

@@ -70,12 +71,10 @@ public function upsert(Entity $entity)
7071
$conditions[$column] = $upsertData[$column];
7172
}
7273

73-
$upsertEntity = $this->_table
74+
return $this->_table
7475
->find()
7576
->where($conditions)
7677
->first();
77-
78-
return $upsertEntity;
7978
}
8079

8180
/**
@@ -85,7 +84,7 @@ public function upsert(Entity $entity)
8584
* @return StatementInterface query result
8685
* @throws LogicException invalid config or no save data
8786
*/
88-
public function bulkUpsert(array $entities)
87+
public function bulkUpsert(array $entities): StatementInterface
8988
{
9089
if (!$this->isValidArrayConfig('updateColumns')) {
9190
throw new LogicException('config updateColumns is invalid.');
@@ -129,7 +128,7 @@ public function bulkUpsert(array $entities)
129128
*
130129
* @return bool valid or invalid
131130
*/
132-
private function isValidArrayConfig($configName)
131+
private function isValidArrayConfig(string $configName): bool
133132
{
134133
$config = $this->_config[$configName];
135134

tests/Fixture/ArticlesFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Itosho\EasyQuery\Test\Fixture;
45

tests/Fixture/TagsFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Itosho\EasyQuery\Test\Fixture;
45

tests/TestCase/Model/Behavior/InsertBehaviorTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Itosho\EasyQuery\Test\TestCase\Model\Behavior;
45

@@ -29,7 +30,7 @@ class InsertBehaviorTest extends TestCase
2930
/**
3031
* {@inheritDoc}
3132
*/
32-
public function setUp()
33+
public function setUp(): void
3334
{
3435
parent::setUp();
3536
$this->Articles = TableRegistry::getTableLocator()->get('Itosho/EasyQuery.Articles');
@@ -39,7 +40,7 @@ public function setUp()
3940
/**
4041
* {@inheritDoc}
4142
*/
42-
public function tearDown()
43+
public function tearDown(): void
4344
{
4445
parent::tearDown();
4546
TableRegistry::getTableLocator()->clear();
@@ -140,12 +141,13 @@ public function testBulkInsertNoBeforeSave()
140141
/**
141142
* bulkInsert() test by no data
142143
*
143-
* @expectedException \LogicException
144-
* @expectedExceptionMessage entities has no save data.
145144
* @return void
146145
*/
147146
public function testBulkInsertNoSaveData()
148147
{
148+
$this->expectExceptionMessage("entities has no save data.");
149+
$this->expectException(\LogicException::class);
150+
149151
$this->Articles->bulkInsert([]);
150152
}
151153

@@ -317,7 +319,7 @@ public function testInsertOnceWhenDuplicatedWithConditions()
317319
*
318320
* @return array
319321
*/
320-
private function getBaseInsertRecords()
322+
private function getBaseInsertRecords(): array
321323
{
322324
return [
323325
[

tests/TestCase/Model/Behavior/UpsertBehaviorTest.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Itosho\EasyQuery\Test\TestCase\Model\Behavior;
45

@@ -28,7 +29,7 @@ class UpsertBehaviorTest extends TestCase
2829
/**
2930
* {@inheritDoc}
3031
*/
31-
public function setUp()
32+
public function setUp(): void
3233
{
3334
parent::setUp();
3435
$this->Tags = TableRegistry::getTableLocator()->get('Itosho/EasyQuery.Tags');
@@ -41,7 +42,7 @@ public function setUp()
4142
/**
4243
* {@inheritDoc}
4344
*/
44-
public function tearDown()
45+
public function tearDown(): void
4546
{
4647
parent::tearDown();
4748
TableRegistry::getTableLocator()->clear();
@@ -237,12 +238,13 @@ public function testUpsertNoBeforeSave()
237238
/**
238239
* upsert() test when invalid update columns
239240
*
240-
* @expectedException \LogicException
241-
* @expectedExceptionMessage config updateColumns is invalid.
242241
* @return void
243242
*/
244243
public function testUpsertInvalidUpdateColumnsConfig()
245244
{
245+
$this->expectExceptionMessage("config updateColumns is invalid.");
246+
$this->expectException(\LogicException::class);
247+
246248
$this->Tags->removeBehavior('Upsert');
247249
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [
248250
'uniqueColumns' => ['name'],
@@ -261,12 +263,13 @@ public function testUpsertInvalidUpdateColumnsConfig()
261263
/**
262264
* upsert() test when invalid unique columns
263265
*
264-
* @expectedException \LogicException
265-
* @expectedExceptionMessage config uniqueColumns is invalid.
266266
* @return void
267267
*/
268268
public function testUpsertInvalidUniqueColumnsConfig()
269269
{
270+
$this->expectExceptionMessage("config uniqueColumns is invalid.");
271+
$this->expectException(\LogicException::class);
272+
270273
$this->Tags->removeBehavior('Upsert');
271274
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [
272275
'updateColumns' => ['description', 'modified'],
@@ -434,12 +437,13 @@ public function testBulkUpsertNoBeforeSave()
434437
/**
435438
* bulkUpsert() test when invalid update columns
436439
*
437-
* @expectedException \LogicException
438-
* @expectedExceptionMessage config updateColumns is invalid.
439440
* @return void
440441
*/
441442
public function testBulkUpsertInvalidUpdateColumnsConfig()
442443
{
444+
$this->expectExceptionMessage("config updateColumns is invalid.");
445+
$this->expectException(\LogicException::class);
446+
443447
$this->Tags->removeBehavior('Upsert');
444448
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert');
445449

@@ -457,12 +461,13 @@ public function testBulkUpsertInvalidUpdateColumnsConfig()
457461
/**
458462
* bulkUpsert() test by no data
459463
*
460-
* @expectedException \LogicException
461-
* @expectedExceptionMessage entities has no save data.
462464
* @return void
463465
*/
464466
public function testBulkUpsertNoSaveData()
465467
{
468+
$this->expectExceptionMessage("entities has no save data.");
469+
$this->expectException(\LogicException::class);
470+
466471
$this->Tags->removeBehavior('Upsert');
467472
$this->Tags->addBehavior('Itosho/EasyQuery.Upsert', [
468473
'updateColumns' => ['description', 'modified'],
@@ -476,7 +481,7 @@ public function testBulkUpsertNoSaveData()
476481
*
477482
* @return array
478483
*/
479-
private function getBaseInsertRecords()
484+
private function getBaseInsertRecords(): array
480485
{
481486
return [
482487
[
@@ -499,7 +504,7 @@ private function getBaseInsertRecords()
499504
*
500505
* @return array
501506
*/
502-
private function getBaseUpdateRecords()
507+
private function getBaseUpdateRecords(): array
503508
{
504509
return [
505510
[

0 commit comments

Comments
 (0)