@@ -73,22 +73,23 @@ public function save(AbstractEntity &$entity): void
73
73
$ changedColumns ['updated_at ' ] = DateTimeHelper::dateTimeToString ($ entity ->updated_at );
74
74
}
75
75
76
- if ($ this ->config ->hasOptimisticLock () && isset ($ entity ->version )) {
77
- $ currentVersion = $ entity ->version ;
76
+
77
+ if ($ this ->config ->hasOptimisticLock ()
78
+ && method_exists ($ entity , 'getVersion ' )
79
+ && method_exists ($ entity , 'incrementVersion ' )) {
80
+ $ where ['lock_version ' ] = $ entity ->getVersion ();
81
+ $ entity ->incrementVersion ();
82
+ $ changedColumns ['lock_version ' ] = $ entity ->getVersion ();
83
+
78
84
try {
79
85
$ connection ->beginTransaction ();
80
- $ connection ->update (
86
+ $ versionUpdated = $ connection ->update (
81
87
$ this ->getTableName (),
82
88
$ changedColumns ,
83
89
$ where
84
90
);
85
- $ versionUpdated = $ connection ->update (
86
- $ this ->getTableName (),
87
- ['version ' => $ currentVersion + 1 ],
88
- $ where + ['version ' => $ currentVersion ]
89
- );
90
91
if (!$ versionUpdated ) {
91
- throw new DbException ('Failed to update entity version, concurrency modification, rolling back. ' );
92
+ throw new Exceptions \ LockException ('Failed to update entity version, concurrency modification, rolling back. ' );
92
93
}
93
94
$ connection ->commit ();
94
95
} catch (\Throwable $ e ) {
@@ -213,13 +214,15 @@ protected function findByPkInternal(mixed $pk): ?array
213
214
214
215
/**
215
216
* @param array<string, mixed> $where
217
+ * @param array<string, string>|string $orderBy
216
218
* @return array<string, mixed>|null
217
219
* @throws \Doctrine\DBAL\Exception
218
220
*/
219
- protected function findOneInternal (array $ where ): ?array
221
+ protected function findOneInternal (array $ where, array | string $ orderBy = [] ): ?array
220
222
{
221
223
$ query = $ this ->select ();
222
224
$ this ->buildWhere ($ query , $ where );
225
+ $ this ->applyOrderBy ($ query , $ orderBy );
223
226
return $ query ->fetchAssociative () ?: null ;
224
227
}
225
228
@@ -259,22 +262,7 @@ protected function findAllInternal(
259
262
$ query ->setParameter ($ param , $ value );
260
263
}
261
264
}
262
- if ($ orderBy ) {
263
- if (is_array ($ orderBy )) {
264
- foreach ($ orderBy as $ column => $ direction ) {
265
- $ query ->addOrderBy ($ column , $ direction );
266
- }
267
- } else {
268
- foreach (explode (', ' , $ orderBy ) as $ orderByPart ) {
269
- $ orderByPart = trim ($ orderByPart );
270
- if (preg_match ('/(.+)\s(asc|desc)$/i ' , $ orderByPart , $ orderByPartMatch )) {
271
- $ query ->addOrderBy ($ orderByPartMatch [1 ], $ orderByPartMatch [2 ]);
272
- } else {
273
- $ query ->addOrderBy ($ orderByPart );
274
- }
275
- }
276
- }
277
- }
265
+ $ this ->applyOrderBy ($ query , $ orderBy );
278
266
if ($ limit > 0 ) {
279
267
$ query ->setMaxResults ($ limit );
280
268
}
@@ -398,4 +386,28 @@ private function formatData(array $data): array
398
386
}
399
387
return $ data ;
400
388
}
389
+
390
+ /**
391
+ * @param array<string, string>|string $orderBy
392
+ */
393
+ private function applyOrderBy (QueryBuilder $ query , string |array $ orderBy ): void
394
+ {
395
+ if (!$ orderBy ) {
396
+ return ;
397
+ }
398
+ if (is_array ($ orderBy )) {
399
+ foreach ($ orderBy as $ column => $ direction ) {
400
+ $ query ->addOrderBy ($ column , $ direction );
401
+ }
402
+ } else {
403
+ foreach (explode (', ' , $ orderBy ) as $ orderByPart ) {
404
+ $ orderByPart = trim ($ orderByPart );
405
+ if (preg_match ('/(.+)\s(asc|desc)$/i ' , $ orderByPart , $ orderByPartMatch )) {
406
+ $ query ->addOrderBy ($ orderByPartMatch [1 ], $ orderByPartMatch [2 ]);
407
+ } else {
408
+ $ query ->addOrderBy ($ orderByPart );
409
+ }
410
+ }
411
+ }
412
+ }
401
413
}
0 commit comments