10
10
use ByJG \Config \Exception \KeyNotFoundException ;
11
11
use ByJG \MicroOrm \Exception \OrmBeforeInvalidException ;
12
12
use ByJG \MicroOrm \Exception \OrmInvalidFieldsException ;
13
- use ByJG \MicroOrm \Exception \RepositoryReadOnlyException ;
14
- use ByJG \MicroOrm \Exception \UpdateConstraintException ;
15
13
use ByJG \MicroOrm \FieldMapping ;
16
14
use ByJG \MicroOrm \Literal \HexUuidLiteral ;
17
15
use ByJG \MicroOrm \Literal \Literal ;
@@ -41,6 +39,11 @@ public function get($itemId)
41
39
return $ this ->repository ->get (HexUuidLiteral::create ($ itemId ));
42
40
}
43
41
42
+ public function getRepository (): Repository
43
+ {
44
+ return $ this ->repository ;
45
+ }
46
+
44
47
public function getMapper ()
45
48
{
46
49
return $ this ->repository ->getMapper ();
@@ -84,7 +87,7 @@ public function listGeneric($tableName, $fields = [], $page = 0, $size = 20, $or
84
87
85
88
$ object = $ query ->build ($ this ->repository ->getDbDriver ());
86
89
87
- $ iterator = $ this ->repository ->getDbDriver ()->getIterator ($ object[ " sql " ] , $ object[ " params " ] );
90
+ $ iterator = $ this ->repository ->getDbDriver ()->getIterator ($ object-> getSql () , $ object-> getParameters () );
88
91
return $ iterator ->toArray ();
89
92
}
90
93
@@ -158,26 +161,31 @@ public static function getUuid()
158
161
protected function setClosureFixBinaryUUID (?Mapper $ mapper , $ binPropertyName = 'id ' , $ uuidStrPropertyName = 'uuid ' )
159
162
{
160
163
$ fieldMapping = FieldMapping::create ($ binPropertyName )
161
- ->withUpdateFunction (function ($ value , $ instance ) {
162
- if (empty ($ value )) {
163
- return null ;
164
- }
165
- if (!($ value instanceof Literal)) {
166
- $ value = new HexUuidLiteral ($ value );
167
- }
168
- return $ value ;
169
- })
170
- ->withSelectFunction (function ($ value , $ instance ) use ($ binPropertyName , $ uuidStrPropertyName ) {
171
- if (!empty ($ uuidStrPropertyName )) {
172
- $ fieldValue = $ instance ->{'get ' . $ uuidStrPropertyName }();
173
- } else {
174
- $ fieldValue = HexUuidLiteral::getFormattedUuid ($ instance ->{'get ' . $ binPropertyName }(), false );
164
+ ->withUpdateFunction (
165
+ function ($ value , $ instance ) {
166
+ if (empty ($ value )) {
167
+ return null ;
168
+ }
169
+ if (!($ value instanceof Literal)) {
170
+ $ value = new HexUuidLiteral ($ value );
171
+ }
172
+ return $ value ;
175
173
}
176
- if (is_null ($ fieldValue )) {
177
- return null ;
174
+ )
175
+ ->withSelectFunction (
176
+ function ($ value , $ instance ) use ($ binPropertyName , $ uuidStrPropertyName ) {
177
+ if (!empty ($ uuidStrPropertyName )) {
178
+ $ fieldValue = $ instance ->{'get ' . $ uuidStrPropertyName }();
179
+ } else {
180
+ $ itemValue = $ instance ->{'get ' . $ binPropertyName }();
181
+ $ fieldValue = HexUuidLiteral::getFormattedUuid ($ itemValue , false , $ itemValue );
182
+ }
183
+ if (is_null ($ fieldValue )) {
184
+ return null ;
185
+ }
186
+ return $ fieldValue ;
178
187
}
179
- return $ fieldValue ;
180
- });
188
+ );
181
189
182
190
if (!empty ($ mapper )) {
183
191
$ mapper ->addFieldMapping ($ fieldMapping );
@@ -187,26 +195,21 @@ protected function setClosureFixBinaryUUID(?Mapper $mapper, $binPropertyName = '
187
195
}
188
196
189
197
/**
190
- * @param $model
191
- * @param UpdateConstraint|null $updateConstraint
198
+ * @param $model
192
199
* @return mixed
193
200
* @throws InvalidArgumentException
194
201
* @throws OrmBeforeInvalidException
195
202
* @throws OrmInvalidFieldsException
196
203
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
197
- * @throws RepositoryReadOnlyException
198
- * @throws UpdateConstraintException
199
204
*/
200
205
public function save ($ model , ?UpdateConstraint $ updateConstraint = null )
201
206
{
202
207
$ model = $ this ->repository ->save ($ model , $ updateConstraint );
203
208
204
209
$ primaryKey = $ this ->repository ->getMapper ()->getPrimaryKey ()[0 ];
205
210
206
- if ($ model ->{"get $ primaryKey " }() instanceof HexUuidLiteral) {
207
- /** @var HexUuidLiteral $literal */
208
- $ literal = $ model ->{"get $ primaryKey " }();
209
- $ model ->{"set $ primaryKey " }($ literal ->formatUuid ());
211
+ if ($ model ->{"get $ primaryKey " }() instanceof Literal) {
212
+ $ model ->{"set $ primaryKey " }(HexUuidLiteral::create ($ model ->{"get $ primaryKey " }()));
210
213
}
211
214
212
215
return $ model ;
0 commit comments