forked from kvnZero/hyperf-APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostMethod.php
More file actions
38 lines (32 loc) · 1.19 KB
/
PostMethod.php
File metadata and controls
38 lines (32 loc) · 1.19 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
<?php
namespace App\ApiJson\Method;
use App\Event\ApiJson\QueryExecuteAfter;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Arr;
use Psr\EventDispatcher\EventDispatcherInterface;
class PostMethod extends AbstractMethod
{
protected function validateCondition(): bool
{
return $this->method == 'POST';
}
protected function process()
{
$insertData = $this->tableEntity->getContent();
$queryMany = $this->isQueryMany();
if (!$queryMany && Arr::isAssoc($insertData)) {
$insertData = [$insertData];
}
if (!$queryMany) {
$insertData = [$insertData[0]];
}
$insertIds = [];
foreach ($insertData as $insertItem) {
$insertIds[] = $this->query->insert($insertItem); //因为需要返回ID 直接insert($insertData)不能得到本次插入的ID 未找到相关可用方法替代
}
$result = $this->parseManyResponse($insertIds, $this->isQueryMany());
$event = new QueryExecuteAfter($this->query->toSql(), $this->method, $result);
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
return $event->result;
}
}