Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DAOBulkUpdatesTrait.php
Go to the documentation of this file.
1<?php
2
4
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
43 public static function toInsert($instance): void {
44 self::toOperation ( $instance, 'insert' );
45 }
46
53 public static function toInserts(array $instances): void {
54 self::toOperations ( $instances, 'insert' );
55 }
56
60 public static function flushInserts(): void {
61 $bulks = self::$bulks ['insert'];
62 foreach ( $bulks as $bulk ) {
63 $bulk->flush ();
64 }
65 }
66
73 public static function toUpdate($instance): void {
74 self::toOperation ( $instance, 'update' );
75 }
76
83 public static function toUpdates(array $instances): void {
84 self::toOperations ( $instances, 'update' );
85 }
86
87 public static function updateGroups($count = 5) {
88 $bulks = self::$bulks ['update'];
89 foreach ( $bulks as $bulk ) {
90 $bulk->groupOp ( $count );
91 }
92 }
93
94 public static function insertGroups($count = 5) {
95 $bulks = self::$bulks ['insert'];
96 foreach ( $bulks as $bulk ) {
97 $bulk->groupOp ( $count );
98 }
99 }
100
101 public static function deleteGroups($count = 5) {
102 $bulks = self::$bulks ['delete'];
103 foreach ( $bulks as $bulk ) {
104 $bulk->groupOp ( $count );
105 }
106 }
107
111 public static function flushUpdates(): void {
112 $bulks = self::$bulks ['update'];
113 foreach ( $bulks as $bulk ) {
114 $bulk->flush ();
115 }
116 }
117
124 public static function toDelete($instance): void {
125 self::toOperation ( $instance, 'delete' );
126 }
127
134 public static function toDeletes(array $instances): void {
135 self::toOperations ( $instances, 'delete' );
136 }
137
141 public static function flushDeletes(): void {
142 $bulks = self::$bulks ['delete'];
143 foreach ( $bulks as $bulk ) {
144 $bulk->flush ();
145 }
146 }
147
151 public static function flush(): void {
152 foreach ( self::$bulks as $bulks ) {
153 foreach ( $bulks as $bulk ) {
154 $bulk->flush ();
155 }
156 }
157 }
158
165 public static function clearBulks(?array $operations = null, ?array $classes = null): void {
166 $operations ??= \array_keys ( self::$bulks );
167 foreach ( $operations as $op ) {
168 $thisClasses = $classes ?? \array_keys ( self::$bulks [$op] );
169 foreach ( $thisClasses as $class ) {
170 if (isset ( self::$bulks [$op] [$class] )) {
171 self::$bulks [$op] [$class]->clear ();
172 }
173 }
174 }
175 }
176
184 public static function countInstancesBulk(string $class, string $operation = 'update'): int {
185 $bulk = self::$bulks [$operation] [$class] ?? null;
186 if (isset ( $bulk )) {
187 return $bulk->count ();
188 }
189 return 0;
190 }
191}
192
Ubiquity\orm\traits$DAOBulkUpdatesTrait This class is part of Ubiquity.
static flush()
Executes all waiting operations (inserts, updates, deletes)
static toUpdate($instance)
Adds an instance in the bulk list of objects to update.
static toUpdates(array $instances)
Adds an array of instances in the bulk list of objects to update.
static flushInserts()
Executes all waiting insert operations.
static flushDeletes()
Executes all waiting delete operations.
static flushUpdates()
Executes all waiting update operations.
static clearBulks(?array $operations=null, ?array $classes=null)
Clear bulk and clear instances waiting for operations.
static toInsert($instance)
Adds an instance in the bulk list of objects to insert.
static toOperation($instance, string $operation)
static toInserts(array $instances)
Adds an array of instances in the bulk list of objects to insert.
static toDelete($instance)
Adds an instance in the bulk list of objects to delete.
static getBulk($class, $operation='update')
static toOperations(array $instances, string $operation)
static toDeletes(array $instances)
Adds an array of instances in the bulk list of objects to delete.
static countInstancesBulk(string $class, string $operation='update')
Return the count of instances waiting for flushing in a bulk.
Ubiquity\orm\traits This class is part of Ubiquity.