-
Notifications
You must be signed in to change notification settings - Fork 0
/
Repository.php
345 lines (310 loc) · 11.5 KB
/
Repository.php
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
namespace pc\entity;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\ActiveQuery;
use yii\db\Expression;
use yii\db\StaleObjectException;
use yii\db\TableSchema;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
class Repository extends BaseRepository
{
/**
* The insert operation. This is mainly used when overriding [[transactions()]] to specify which operations are transactional.
*/
const OP_INSERT = 0x01;
/**
* The update operation. This is mainly used when overriding [[transactions()]] to specify which operations are transactional.
*/
const OP_UPDATE = 0x02;
/**
* The delete operation. This is mainly used when overriding [[transactions()]] to specify which operations are transactional.
*/
const OP_DELETE = 0x04;
/**
* All three operations: insert, update, delete.
* This is a shortcut of the expression: OP_INSERT | OP_UPDATE | OP_DELETE.
*/
const OP_ALL = 0x07;
/**
* @inheritdoc
*/
public function getDb()
{
return Yii::$app->getDb();
}
/**
* @inheritdoc
*/
public function primaryKey()
{
return $this->getTableSchema()->primaryKey;
}
/**
* Returns the schema information of the DB table associated with this AR class.
* @return TableSchema the schema information of the DB table associated with this AR class.
* @throws InvalidConfigException if the table for the AR class does not exist.
*/
public function getTableSchema()
{
$schema = $this->getDb()->getSchema()->getTableSchema($this->tableName());
if ($schema !== null) {
return $schema;
} else {
throw new InvalidConfigException("The table does not exist: " . $this->tableName());
}
}
/**
* @inheritdoc
*/
public function find()
{
return Yii::createObject(ActiveQuery::className(), [$this]);
}
/**
* Declares the name of the database table associated with this AR class.
* By default this method returns the class name as the table name by calling [[Inflector::camel2id()]]
* with prefix [[Connection::tablePrefix]]. For example if [[Connection::tablePrefix]] is 'tbl_',
* 'Customer' becomes 'tbl_customer', and 'OrderItem' becomes 'tbl_order_item'. You may override this method
* if the table is not named after this convention.
* @return string the table name
*/
public function tableName()
{
return '{{%' . Inflector::camel2id(StringHelper::basename(get_called_class()), '_') . '}}';
}
/**
* @inheritdoc
*/
public function updateAllCounters($counters, $condition = '', $params = [])
{
$n = 0;
foreach ($counters as $name => $value) {
$counters[$name] = new Expression("[[$name]]+:bp{$n}", [":bp{$n}" => $value]);
$n++;
}
$command = $this->getDb()->createCommand();
$command->update($this->tableName(), $counters, $condition, $params);
return $command->execute();
}
/**
* @inheritdoc
*/
public function updateAll($attributes, $condition = '', $params = [])
{
$command = $this->getDb()->createCommand();
$command->update($this->tableName(), $attributes, $condition, $params);
return $command->execute();
}
/**
* @inheritdoc
*/
public function deleteAll($condition = '', $params = [])
{
$command = $this->getDb()->createCommand();
$command->delete($this->tableName(), $condition, $params);
return $command->execute();
}
/**
* @inheritdoc
*/
public function update($entity, $attributeNames = null)
{
if (!$entity->validate($attributeNames)) {
Yii::info('Model not updated due to validation error.', __METHOD__);
return false;
}
if (!$this->isTransactional($entity, self::OP_UPDATE)) {
return $this->updateInternal($attributeNames);
}
$transaction = $this->getDb()->beginTransaction();
try {
$result = $this->updateInternal($entity, $attributeNames);
if ($result === false) {
$transaction->rollBack();
} else {
$transaction->commit();
}
return $result;
} catch (\Exception $e) {
$transaction->rollBack();
throw $e;
}
}
/**
* @inheritdoc
*/
public function insert($entity, $attributes = null)
{
if (!$entity->validate($attributes)) {
Yii::info('Model not inserted due to validation error.', __METHOD__);
return false;
}
if (!$this->isTransactional($entity, self::OP_INSERT)) {
return $this->insertInternal($entity, $attributes);
}
$transaction = $this->getDb()->beginTransaction();
try {
$result = $this->insertInternal($entity, $attributes);
if ($result === false) {
$transaction->rollBack();
} else {
$transaction->commit();
}
return $result;
} catch (\Exception $e) {
$transaction->rollBack();
throw $e;
}
}
/**
* Inserts an ActiveRecord into DB without considering transaction.
* @param Entity $entity
* @param array $attributes list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @return boolean whether the record is inserted successfully.
*/
protected function insertInternal($entity, $attributes = null)
{
if (!$this->beforeSave($entity, true)) {
return false;
}
$values = $entity->getDirtyAttributes($attributes);
if (($primaryKeys = static::getDb()->schema->insert($this->tableName(), $values)) === false) {
return false;
}
foreach ($primaryKeys as $name => $value) {
$id = $this->getTableSchema()->columns[$name]->phpTypecast($value);
$entity->setAttribute($name, $id);
$values[$name] = $id;
}
$changedAttributes = array_fill_keys(array_keys($values), null);
$entity->setOldAttributes($values);
$this->afterSave($entity, true, $changedAttributes);
return true;
}
/**
* Deletes the table row corresponding to this active record.
*
* This method performs the following steps in order:
*
* 1. call [[beforeDelete()]]. If the method returns false, it will skip the
* rest of the steps;
* 2. delete the record from the database;
* 3. call [[afterDelete()]].
*
* In the above step 1 and 3, events named [[EVENT_BEFORE_DELETE]] and [[EVENT_AFTER_DELETE]]
* will be raised by the corresponding methods.
*
* @param Entity $entity
* @return integer|false the number of rows deleted, or false if the deletion is unsuccessful for some reason.
* Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
* @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
* being deleted is outdated.
* @throws \Exception in case delete failed.
*/
public function delete($entity)
{
if (!$this->isTransactional($entity,self::OP_DELETE)) {
return $this->deleteInternal($entity);
}
$transaction = static::getDb()->beginTransaction();
try {
$result = $this->deleteInternal($entity);
if ($result === false) {
$transaction->rollBack();
} else {
$transaction->commit();
}
return $result;
} catch (\Exception $e) {
$transaction->rollBack();
throw $e;
}
}
/**
* Deletes an ActiveRecord without considering transaction.
* @param Entity $entity
* @return integer|false the number of rows deleted, or false if the deletion is unsuccessful for some reason.
* Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
* @throws StaleObjectException
*/
protected function deleteInternal($entity)
{
if (!$this->beforeDelete($entity)) {
return false;
}
// we do not check the return value of deleteAll() because it's possible
// the record is already deleted in the database and thus the method will return 0
$condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock();
if ($lock !== null) {
$condition[$lock] = $this->$lock;
}
$result = $this->deleteAll($condition);
if ($lock !== null && !$result) {
throw new StaleObjectException('The object being deleted is outdated.');
}
$entity->setOldAttributes(null);
$this->afterDelete($entity);
return $result;
}
/**
* Returns a value indicating whether the specified operation is transactional in the current [[scenario]].
* @param Entity $entity
* @param integer $operation the operation to check. Possible values are [[OP_INSERT]], [[OP_UPDATE]] and [[OP_DELETE]].
* @return boolean whether the specified operation is transactional in the current [[scenario]].
*/
public function isTransactional($entity, $operation)
{
$scenario = $entity->getScenario();
$transactions = $this->transactions();
return isset($transactions[$scenario]) && ($transactions[$scenario] & $operation);
}
/**
* Declares which DB operations should be performed within a transaction in different scenarios.
* The supported DB operations are: [[OP_INSERT]], [[OP_UPDATE]] and [[OP_DELETE]],
* which correspond to the [[insert()]], [[update()]] and [[delete()]] methods, respectively.
* By default, these methods are NOT enclosed in a DB transaction.
*
* In some scenarios, to ensure data consistency, you may want to enclose some or all of them
* in transactions. You can do so by overriding this method and returning the operations
* that need to be transactional. For example,
*
* ```php
* return [
* 'admin' => self::OP_INSERT,
* 'api' => self::OP_INSERT | self::OP_UPDATE | self::OP_DELETE,
* // the above is equivalent to the following:
* // 'api' => self::OP_ALL,
*
* ];
* ```
*
* The above declaration specifies that in the "admin" scenario, the insert operation ([[insert()]])
* should be done in a transaction; and in the "api" scenario, all the operations should be done
* in a transaction.
*
* @return array the declarations of transactional operations. The array keys are scenarios names,
* and the array values are the corresponding transaction operations.
*/
public function transactions()
{
return [];
}
/**
* @inheritdoc
*/
public function equals($source, $target)
{
if ($source->getIsNewRecord() || $target->getIsNewRecord()) {
return false;
}
/** @var RepositoryInterface $sourceRepo */
$sourceRepo = $this->entityManager->getRepository($source::className());
/** @var RepositoryInterface $sourceRepo */
$targetRepo = $this->entityManager->getRepository($target::className());
return get_class($sourceRepo) === get_class($targetRepo) && $sourceRepo->getPrimaryKey($source) === $targetRepo->getPrimaryKey($target);
}
}