Skip to content

Commit 79028e7

Browse files
committed
fix for #744
1 parent 9f988e8 commit 79028e7

File tree

5 files changed

+72
-11
lines changed

5 files changed

+72
-11
lines changed

src/Tqdev/PhpCrudApi/Controller/RecordController.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ public function read(ServerRequestInterface $request): ResponseInterface
6060
}
6161
}
6262

63+
private function multiCall(callable $method, array $argumentLists): array
64+
{
65+
$result = array();
66+
$success = true;
67+
$this->service->beginTransaction();
68+
foreach ($argumentLists as $arguments) {
69+
try {
70+
$result[] = call_user_func_array($method, $arguments);
71+
} catch (\Throwable $e) {
72+
$success = false;
73+
$result[] = null;
74+
}
75+
}
76+
if ($success) {
77+
$this->service->commitTransaction();
78+
} else {
79+
$this->service->rollBackTransaction();
80+
}
81+
return $result;
82+
}
83+
6384
public function create(ServerRequestInterface $request): ResponseInterface
6485
{
6586
$table = RequestUtils::getPathSegment($request, 2);
@@ -75,11 +96,11 @@ public function create(ServerRequestInterface $request): ResponseInterface
7596
}
7697
$params = RequestUtils::getParams($request);
7798
if (is_array($record)) {
78-
$result = array();
99+
$argumentLists = array();
79100
foreach ($record as $r) {
80-
$result[] = $this->service->create($table, $r, $params);
101+
$argumentLists[] = array($table, $r, $params);
81102
}
82-
return $this->responder->success($result);
103+
return $this->responder->success($this->multiCall([$this->service, 'create'], $argumentLists));
83104
} else {
84105
return $this->responder->success($this->service->create($table, $record, $params));
85106
}
@@ -105,11 +126,11 @@ public function update(ServerRequestInterface $request): ResponseInterface
105126
if (count($ids) != count($record)) {
106127
return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
107128
}
108-
$result = array();
129+
$argumentLists = array();
109130
for ($i = 0; $i < count($ids); $i++) {
110-
$result[] = $this->service->update($table, $ids[$i], $record[$i], $params);
131+
$argumentLists[] = array($table, $ids[$i], $record[$i], $params);
111132
}
112-
return $this->responder->success($result);
133+
return $this->responder->success($this->multiCall([$this->service, 'update'], $argumentLists));
113134
} else {
114135
if (count($ids) != 1) {
115136
return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);

src/Tqdev/PhpCrudApi/Database/GenericDB.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ public function definition(): GenericDefinition
160160
return $this->definition;
161161
}
162162

163+
public function beginTransaction() /*: void*/
164+
{
165+
$this->pdo->beginTransaction();
166+
}
167+
168+
public function commitTransaction() /*: void*/
169+
{
170+
$this->pdo->commit();
171+
}
172+
173+
public function rollBackTransaction() /*: void*/
174+
{
175+
$this->pdo->rollBack();
176+
}
177+
163178
private function addMiddlewareConditions(string $tableName, Condition $condition): Condition
164179
{
165180
$condition1 = VariableStore::get("authorization.conditions.$tableName");
@@ -334,7 +349,7 @@ public function getCacheKey(): string
334349
$this->port,
335350
$this->database,
336351
$this->tables,
337-
$this->username
352+
$this->username,
338353
]));
339354
}
340355
}

src/Tqdev/PhpCrudApi/Record/RecordService.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ public function getType(string $table): string
5656
return $this->reflection->getType($table);
5757
}
5858

59+
public function beginTransaction() /*: void*/
60+
{
61+
$this->db->beginTransaction();
62+
}
63+
64+
public function commitTransaction() /*: void*/
65+
{
66+
$this->db->commitTransaction();
67+
}
68+
69+
public function rollBackTransaction() /*: void*/
70+
{
71+
$this->db->rollBackTransaction();
72+
}
73+
5974
public function create(string $tableName, /* object */ $record, array $params) /*: ?int*/
6075
{
6176
$this->sanitizeRecord($tableName, $record, '');

tests/functional/001_records/090_add_multiple_comments.log

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ POST /records/comments
2121

2222
[{"user_id":1,"post_id":6,"message":"multi 3","category_id":3},{"user_id":1,"post_id":0,"message":"multi 4","category_id":3}]
2323
===
24+
200
25+
Content-Type: application/json; charset=utf-8
26+
Content-Length: 8
27+
28+
[9,null]
2429
===
2530
GET /records/comments?include=id&filter=post_id,eq,6
2631
===
32+
200
33+
Content-Type: application/json; charset=utf-8
34+
Content-Length: 31
35+
36+
{"records":[{"id":7},{"id":8}]}

tests/functional/001_records/091_edit_multiple_comments.log

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ PUT /records/comments/7,8
2323
===
2424
200
2525
Content-Type: application/json; charset=utf-8
26-
Content-Length: 5
26+
Content-Length: 8
2727

28-
[0,0]
28+
[1,null]
2929
===
3030
GET /records/comments?include=message&filter=post_id,eq,6
3131
===
3232
200
3333
Content-Type: application/json; charset=utf-8
34-
Content-Length: 14
34+
Content-Length: 57
3535

36-
{"records":[]}
36+
{"records":[{"message":"multi 1"},{"message":"multi 2"}]}

0 commit comments

Comments
 (0)