|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the "RocketORM" package. |
|
5
|
|
|
* |
|
6
|
|
|
* https://github.com/RocketORM/ORM |
|
7
|
|
|
* |
|
8
|
|
|
* For the full license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Rocket\ORM\Model; |
|
13
|
|
|
|
|
14
|
|
|
use Rocket\ORM\Connection\ConnectionInterface; |
|
15
|
|
|
use Rocket\ORM\Model\Map\TableMapInterface; |
|
16
|
|
|
use Rocket\ORM\Rocket; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Sylvain Lorinet <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class Model implements ModelInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_isNew = true; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $_isModified = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_isDeleted = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var TableMapInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $tableMap; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \PDO $con |
|
|
|
|
|
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
*/ |
|
51
|
7 |
|
public function save(\PDO $con = null) |
|
52
|
|
|
{ |
|
53
|
7 |
|
if ($this->_isDeleted) { |
|
54
|
1 |
|
throw new \LogicException('Cannot save a deleted object'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
6 |
|
if (null == $con) { |
|
58
|
5 |
|
$con = Rocket::getConnection($this->getTableMap()->getConnectionName(), ConnectionInterface::CONNECTION_MODE_WRITE); |
|
59
|
5 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
6 |
|
if ($this->preSave($con)) { |
|
|
|
|
|
|
63
|
6 |
|
if ($this->_isNew) { |
|
64
|
5 |
|
$this->doInsert($con); |
|
|
|
|
|
|
65
|
3 |
|
$this->postSave($con); |
|
|
|
|
|
|
66
|
4 |
|
} elseif ($this->_isModified) { |
|
67
|
1 |
|
if ($this->_isModified) { |
|
68
|
1 |
|
$this->doUpdate($con); |
|
|
|
|
|
|
69
|
1 |
|
$this->postSave($con); |
|
|
|
|
|
|
70
|
1 |
|
} |
|
71
|
1 |
|
} |
|
72
|
4 |
|
} |
|
73
|
6 |
|
} catch (\Exception $e) { |
|
74
|
2 |
|
if ($con->inTransaction()) { |
|
|
|
|
|
|
75
|
1 |
|
$con->rollBack(); |
|
|
|
|
|
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
throw $e; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
$this->_isNew = false; |
|
82
|
4 |
|
$this->_isModified = false; |
|
83
|
|
|
|
|
84
|
4 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param \PDO $con |
|
|
|
|
|
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
* |
|
92
|
|
|
* @throws \Exception |
|
93
|
|
|
*/ |
|
94
|
4 |
|
public function delete(\PDO $con = null) |
|
95
|
|
|
{ |
|
96
|
4 |
|
if ($this->_isNew) { |
|
97
|
1 |
|
throw new \LogicException('Cannot delete a new object'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
if ($this->_isDeleted) { |
|
101
|
1 |
|
throw new \LogicException('Cannot delete an already deleted object'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
if (null == $con) { |
|
105
|
1 |
|
$con = Rocket::getConnection($this->getTableMap()->getConnectionName(), ConnectionInterface::CONNECTION_MODE_WRITE); |
|
106
|
1 |
|
} |
|
107
|
|
|
try { |
|
108
|
2 |
|
if ($this->preDelete($con)) { |
|
|
|
|
|
|
109
|
2 |
|
$this->doDelete($con); |
|
|
|
|
|
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
$this->postDelete($con); |
|
|
|
|
|
|
113
|
2 |
|
} catch (\Exception $e) { |
|
114
|
1 |
|
if ($con->inTransaction()) { |
|
|
|
|
|
|
115
|
1 |
|
$con->rollBack(); |
|
|
|
|
|
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
throw $e; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
$this->_isDeleted = true; |
|
122
|
|
|
|
|
123
|
1 |
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param \PDO $con |
|
128
|
|
|
* |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
7 |
|
protected function preSave(\PDO $con) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
7 |
|
return true; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param \PDO $con |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
5 |
|
protected function postSave(\PDO $con) |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
5 |
|
return true; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param \PDO $con |
|
148
|
|
|
* |
|
149
|
|
|
* @return bool |
|
150
|
|
|
*/ |
|
151
|
3 |
|
protected function preDelete(\PDO $con) |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
3 |
|
return true; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param \PDO $con |
|
158
|
|
|
* |
|
159
|
|
|
* @return bool |
|
160
|
|
|
*/ |
|
161
|
2 |
|
protected function postDelete(\PDO $con) |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
2 |
|
return true; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return TableMapInterface |
|
168
|
|
|
*/ |
|
169
|
4 |
|
protected function getTableMap() |
|
170
|
|
|
{ |
|
171
|
4 |
|
if (!isset($this->tableMap)) { |
|
172
|
2 |
|
$this->tableMap = Rocket::getTableMap(get_called_class()); |
|
173
|
2 |
|
} |
|
174
|
|
|
|
|
175
|
4 |
|
return $this->tableMap; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param \PDO $con |
|
180
|
|
|
* |
|
181
|
|
|
* @return void |
|
182
|
|
|
*/ |
|
183
|
|
|
protected abstract function doInsert(\PDO $con); |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param \PDO $con |
|
187
|
|
|
* |
|
188
|
|
|
* @return void |
|
189
|
|
|
*/ |
|
190
|
|
|
protected abstract function doUpdate(\PDO $con); |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param \PDO $con |
|
194
|
|
|
* |
|
195
|
|
|
* @return void |
|
196
|
|
|
*/ |
|
197
|
|
|
protected abstract function doDelete(\PDO $con); |
|
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.