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.0 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
trait DAOBulkUpdatesTrait { |
14
|
|
|
protected static $bulks = [ 'insert' => [ ],'update' => [ ],'delete' => [ ] ]; |
15
|
|
|
|
16
|
|
|
protected static function getBulk($instance, $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 ( $instance, $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 ( $instance, $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
|
|
|
public static function toInserts(array $instances): void { |
48
|
|
|
self::toOperations ( $instances, 'insert' ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function flushInserts(): void { |
52
|
|
|
$bulks = self::$bulks ['insert']; |
53
|
|
|
foreach ( $bulks as $bulk ) { |
54
|
|
|
$bulk->flush (); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function toUpdate(object $instance): void { |
59
|
|
|
self::toOperation ( $instance, 'update' ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function toUpdates(array $instances): void { |
63
|
|
|
self::toOperations ( $instances, 'update' ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function flushUpdates(): void { |
67
|
|
|
$bulks = self::$bulks ['update']; |
68
|
|
|
foreach ( $bulks as $bulk ) { |
69
|
|
|
$bulk->flush (); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function toDelete(object $instance): void { |
74
|
|
|
self::toOperation ( $instance, 'delete' ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function toDeletes(array $instances): void { |
78
|
|
|
self::toOperations ( $instances, 'delete' ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function flushDeletes(): void { |
82
|
|
|
$bulks = self::$bulks ['delete']; |
83
|
|
|
foreach ( $bulks as $bulk ) { |
84
|
|
|
$bulk->flush (); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function flush(): void { |
89
|
|
|
foreach ( self::$bulks as $bulks ) { |
90
|
|
|
foreach ( $bulks as $bulk ) { |
91
|
|
|
$bulk->flush (); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.