|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\ActiveRecord\Trait; |
|
6
|
|
|
|
|
7
|
|
|
use Throwable; |
|
8
|
|
|
use Yiisoft\ActiveRecord\ActiveRecordInterface; |
|
9
|
|
|
use Yiisoft\ActiveRecord\TransactionalInterface; |
|
10
|
|
|
|
|
11
|
|
|
use function in_array; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Trait to implement transactional operations and {@see TransactionalInterface} for ActiveRecord. |
|
15
|
|
|
* |
|
16
|
|
|
* @see ActiveRecordInterface::insert() |
|
17
|
|
|
* @see ActiveRecordInterface::update() |
|
18
|
|
|
* @see ActiveRecordInterface::delete() |
|
19
|
|
|
*/ |
|
20
|
|
|
trait TransactionalTrait |
|
21
|
|
|
{ |
|
22
|
|
|
public function delete(): int |
|
23
|
|
|
{ |
|
24
|
|
|
if (!$this->isTransactional(TransactionalInterface::OP_DELETE)) { |
|
25
|
|
|
return $this->deleteInternal(); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$transaction = $this->db->beginTransaction(); |
|
29
|
|
|
|
|
30
|
|
|
try { |
|
31
|
|
|
$result = $this->deleteInternal(); |
|
32
|
|
|
$transaction->commit(); |
|
33
|
|
|
|
|
34
|
|
|
return $result; |
|
35
|
|
|
} catch (Throwable $e) { |
|
36
|
|
|
$transaction->rollBack(); |
|
37
|
|
|
throw $e; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function insert(array $attributes = null): bool |
|
42
|
|
|
{ |
|
43
|
|
|
if (!$this->isTransactional(TransactionalInterface::OP_INSERT)) { |
|
44
|
|
|
return $this->insertInternal($attributes); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$transaction = $this->db->beginTransaction(); |
|
48
|
|
|
|
|
49
|
|
|
try { |
|
50
|
|
|
$result = $this->insertInternal($attributes); |
|
51
|
|
|
if ($result === false) { |
|
52
|
|
|
$transaction->rollBack(); |
|
53
|
|
|
} else { |
|
54
|
|
|
$transaction->commit(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $result; |
|
58
|
|
|
} catch (Throwable $e) { |
|
59
|
|
|
$transaction->rollBack(); |
|
60
|
|
|
throw $e; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function isTransactional(int $operation): bool |
|
65
|
|
|
{ |
|
66
|
|
|
return in_array($operation, $this->transactions(), true); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function transactions(): array |
|
70
|
|
|
{ |
|
71
|
|
|
return [ |
|
72
|
|
|
TransactionalInterface::OP_INSERT, |
|
73
|
|
|
TransactionalInterface::OP_UPDATE, |
|
74
|
|
|
TransactionalInterface::OP_DELETE, |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function update(array $attributeNames = null): int |
|
79
|
|
|
{ |
|
80
|
|
|
if (!$this->isTransactional(TransactionalInterface::OP_UPDATE)) { |
|
81
|
|
|
return $this->updateInternal($attributeNames); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$transaction = $this->db->beginTransaction(); |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
|
|
$result = $this->updateInternal($attributeNames); |
|
88
|
|
|
if ($result === 0) { |
|
89
|
|
|
$transaction->rollBack(); |
|
90
|
|
|
} else { |
|
91
|
|
|
$transaction->commit(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $result; |
|
95
|
|
|
} catch (Throwable $e) { |
|
96
|
|
|
$transaction->rollBack(); |
|
97
|
|
|
throw $e; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.