forked from kvnZero/hyperf-APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteMethod.php
More file actions
47 lines (40 loc) · 1.37 KB
/
DeleteMethod.php
File metadata and controls
47 lines (40 loc) · 1.37 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
<?php
namespace App\ApiJson\Method;
use App\ApiJson\Interface\QueryInterface;
use App\Event\ApiJson\QueryExecuteAfter;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Arr;
use Psr\EventDispatcher\EventDispatcherInterface;
class DeleteMethod extends AbstractMethod
{
protected function validateCondition(): bool
{
return $this->method == 'DELETE';
}
protected function process()
{
$ids = [];
$jsonData = $this->tableEntity->getContent();
$queryMany = $this->isQueryMany();
if (isset($jsonData['id'])) {
if (is_array($jsonData['id'])) {
$ids = $jsonData['id'];
$queryMany = true;
} else {
$ids = [$jsonData['id']];
}
} else if (isset($jsonData['id{}'])) {
$ids = $jsonData['id{}']; //得到本次需要删除的ID
$queryMany = true;
}
$deletedIds = [];
foreach ($ids as $id) {
$this->buildQuery();
$this->query->delete($id) && $deletedIds[] = $id; //这里主键应可配置
}
$result = $this->parseManyResponse($deletedIds, $queryMany);
$event = new QueryExecuteAfter($this->query->toSql(), $this->method, $result);
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
return $result;
}
}