1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Ubiquity\orm\traits$DAOBulkUpdatesTrait |
7
|
|
|
* This class is part of Ubiquity |
8
|
|
|
* |
9
|
|
|
* @author jcheron <[email protected]> |
10
|
|
|
* @version 1.0.2 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
trait DAOBulkUpdatesTrait { |
14
|
|
|
protected static $bulks = [ 'insert' => [ ],'update' => [ ],'delete' => [ ] ]; |
15
|
|
|
|
16
|
|
|
protected static function getBulk($class, $operation = 'update') { |
17
|
|
|
if (! isset ( self::$bulks [$operation] [$class] )) { |
18
|
|
|
$bulkClass = '\\Ubiquity\\orm\\bulk\\Bulk' . \ucfirst ( $operation ) . 's'; |
19
|
|
|
self::$bulks [$operation] [$class] = new $bulkClass ( $class ); |
20
|
|
|
} |
21
|
|
|
return self::$bulks [$operation] [$class]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected static function toOperation($instance, string $operation): void { |
25
|
|
|
$class = \get_class ( $instance ); |
26
|
|
|
self::getBulk ( $class, $operation )->addInstance ( $instance ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected static function toOperations(array $instances, string $operation): void { |
30
|
|
|
$instance = \current ( $instances ); |
31
|
|
|
if (isset ( $instance )) { |
32
|
|
|
$class = \get_class ( $instance ); |
33
|
|
|
self::getBulk ( $class, $operation )->addInstances ( $instances ); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Adds an instance in the bulk list of objects to insert. |
39
|
|
|
* Call flush to commit the operation |
40
|
|
|
* |
41
|
|
|
* @param object $instance |
42
|
|
|
*/ |
43
|
|
|
public static function toInsert(object $instance): void { |
44
|
|
|
self::toOperation ( $instance, 'insert' ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Adds an array of instances in the bulk list of objects to insert. |
49
|
|
|
* Call flush to commit the operation |
50
|
|
|
* |
51
|
|
|
* @param array $instances |
52
|
|
|
*/ |
53
|
|
|
public static function toInserts(array $instances): void { |
54
|
|
|
self::toOperations ( $instances, 'insert' ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Executes all waiting insert operations |
59
|
|
|
*/ |
60
|
|
|
public static function flushInserts(): void { |
61
|
|
|
$bulks = self::$bulks ['insert']; |
62
|
|
|
foreach ( $bulks as $bulk ) { |
63
|
|
|
$bulk->flush (); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Adds an instance in the bulk list of objects to update. |
69
|
|
|
* Call flush to commit the operation |
70
|
|
|
* |
71
|
|
|
* @param object $instance |
72
|
|
|
*/ |
73
|
|
|
public static function toUpdate(object $instance): void { |
74
|
|
|
self::toOperation ( $instance, 'update' ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Adds an array of instances in the bulk list of objects to update. |
79
|
|
|
* Call flush to commit the operation |
80
|
|
|
* |
81
|
|
|
* @param array $instances |
82
|
|
|
*/ |
83
|
|
|
public static function toUpdates(array $instances): void { |
84
|
|
|
self::toOperations ( $instances, 'update' ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function updateGroups() { |
88
|
|
|
$bulks = self::$bulks ['update']; |
89
|
|
|
foreach ( $bulks as $bulk ) { |
90
|
|
|
$bulk->updateGroup (); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Executes all waiting update operations |
96
|
|
|
*/ |
97
|
|
|
public static function flushUpdates(): void { |
98
|
|
|
$bulks = self::$bulks ['update']; |
99
|
|
|
foreach ( $bulks as $bulk ) { |
100
|
|
|
$bulk->flush (); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Adds an instance in the bulk list of objects to delete. |
106
|
|
|
* Call flush to commit the operation |
107
|
|
|
* |
108
|
|
|
* @param object $instance |
109
|
|
|
*/ |
110
|
|
|
public static function toDelete(object $instance): void { |
111
|
|
|
self::toOperation ( $instance, 'delete' ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Adds an array of instances in the bulk list of objects to delete. |
116
|
|
|
* Call flush to commit the operation |
117
|
|
|
* |
118
|
|
|
* @param array $instances |
119
|
|
|
*/ |
120
|
|
|
public static function toDeletes(array $instances): void { |
121
|
|
|
self::toOperations ( $instances, 'delete' ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Executes all waiting delete operations |
126
|
|
|
*/ |
127
|
|
|
public static function flushDeletes(): void { |
128
|
|
|
$bulks = self::$bulks ['delete']; |
129
|
|
|
foreach ( $bulks as $bulk ) { |
130
|
|
|
$bulk->flush (); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Executes all waiting operations (inserts, updates, deletes) |
136
|
|
|
*/ |
137
|
|
|
public static function flush(): void { |
138
|
|
|
foreach ( self::$bulks as $bulks ) { |
139
|
|
|
foreach ( $bulks as $bulk ) { |
140
|
|
|
$bulk->flush (); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|