forked from kvnZero/hyperf-APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPutMethod.php
More file actions
54 lines (47 loc) · 1.78 KB
/
PutMethod.php
File metadata and controls
54 lines (47 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace App\ApiJson\Method;
use App\Event\ApiJson\QueryExecuteAfter;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Arr;
use Psr\EventDispatcher\EventDispatcherInterface;
class PutMethod extends AbstractMethod
{
protected function validateCondition(): bool
{
return $this->method == 'PUT';
}
protected function process()
{
$updateData = $this->tableEntity->getContent();
$queryMany = $this->isQueryMany();
if (!$queryMany || Arr::isAssoc($updateData)) {
$updateData = [$updateData];
}
$updateIds = [];
foreach ($updateData as $updateItem) {
$ids = [];
if (isset($updateItem['id'])) {
if (is_array($updateItem['id'])) {
$ids = $updateItem['id'];
$queryMany = true;
} else {
$ids = [$updateItem['id']];
}
} else if (isset($updateItem['id{}'])) {
$ids = $updateItem['id{}']; //得到本次需要更新的ID
$queryMany = true;
}
unset($updateItem['id'], $updateItem['id{}']);
foreach ($ids as $id) {
$this->buildQuery();
$querySql = sprintf('`%s` = ?', $this->query->getPrimaryKey());
$this->tableEntity->getConditionEntity()->addQueryWhere('id', $querySql, [$id]);
$this->query->update($updateItem) && $updateIds[] = $id;
}
}
$result = $this->parseManyResponse($updateIds, $queryMany);
$event = new QueryExecuteAfter($this->query->toSql(), $this->method, $result);
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
return $event->result;
}
}