13
13
14
14
namespace App \Base \Abstracts \Models ;
15
15
16
+ use App \App ;
16
17
use ArrayAccess ;
17
18
use DebugBar \DebugBar ;
18
19
use Degami \Basics \Exceptions \BasicException ;
@@ -134,14 +135,14 @@ private function checkDbName(Row $db_row): BaseModel
134
135
/**
135
136
* returns an array of models, starting from a statement Result
136
137
*
137
- * @param ContainerInterface $container
138
138
* @param Result $stmt
139
139
* @return array
140
140
* @throws DependencyException
141
141
* @throws NotFoundException
142
142
*/
143
- public static function hydrateStatementResult (ContainerInterface $ container , Result $ stmt ): array
143
+ public static function hydrateStatementResult (Result $ stmt ): array
144
144
{
145
+ $ container = App::getInstance ()->getContainer ();
145
146
return array_map (
146
147
function ($ el ) use ($ container ) {
147
148
return $ container ->make (static ::class, ['db_row ' => $ el ]);
@@ -153,25 +154,25 @@ function ($el) use ($container) {
153
154
/**
154
155
* basic select statement
155
156
*
156
- * @param ContainerInterface $container
157
157
* @param array $options
158
158
* @return PDOStatement
159
159
*/
160
- public static function select (ContainerInterface $ container , $ options = []): PDOStatement
160
+ public static function select ($ options = []): PDOStatement
161
161
{
162
+ $ container = App::getInstance ()->getContainer ();
162
163
return $ container ->get ('db ' )->select (static ::defaultTableName (), $ options );
163
164
}
164
165
165
166
/**
166
167
* gets basic where statement for model
167
168
*
168
- * @param ContainerInterface $container
169
169
* @param array $condition
170
170
* @param array $order
171
171
* @return Result
172
172
*/
173
- protected static function getModelBasicWhere (ContainerInterface $ container , $ condition = [], $ order = []): Result
173
+ protected static function getModelBasicWhere ($ condition = [], $ order = []): Result
174
174
{
175
+ $ container = App::getInstance ()->getContainer ();
175
176
if ($ condition == null ) {
176
177
$ condition = [];
177
178
}
@@ -228,15 +229,16 @@ protected static function getModelBasicWhere(ContainerInterface $container, $con
228
229
/**
229
230
* returns all found items
230
231
*
231
- * @param ContainerInterface $container
232
232
* @param array $condition
233
233
* @param array $order
234
234
* @return array
235
235
* @throws DependencyException
236
236
* @throws NotFoundException
237
237
*/
238
- public static function all (ContainerInterface $ container , $ condition = [], $ order = []): array
238
+ public static function all ($ condition = [], $ order = []): array
239
239
{
240
+ $ container = App::getInstance ()->getContainer ();
241
+
240
242
/** @var DebugBar $debugbar */
241
243
$ debugbar = $ container ->get ('debugbar ' );
242
244
@@ -246,7 +248,7 @@ public static function all(ContainerInterface $container, $condition = [], $orde
246
248
$ debugbar ['time ' ]->startMeasure ($ measure_key );
247
249
}
248
250
249
- $ items = static ::hydrateStatementResult ($ container , static ::getModelBasicWhere ($ container , $ condition , $ order ));
251
+ $ items = static ::hydrateStatementResult (static ::getModelBasicWhere ($ condition , $ order ));
250
252
251
253
foreach ($ items as $ item ) {
252
254
static ::$ loadedObjects [static ::defaultTableName ()][$ item ->id ] = $ item ;
@@ -262,21 +264,19 @@ public static function all(ContainerInterface $container, $condition = [], $orde
262
264
/**
263
265
* gets total number of elements
264
266
*
265
- * @param ContainerInterface $container
266
267
* @param array $condition
267
268
* @return int
268
269
*/
269
- public static function totalNum (ContainerInterface $ container , $ condition = []): int
270
+ public static function totalNum ($ condition = []): int
270
271
{
271
- $ stmt = static ::getModelBasicWhere ($ container , $ condition );
272
+ $ stmt = static ::getModelBasicWhere ($ condition );
272
273
273
274
return $ stmt ->count ();
274
275
}
275
276
276
277
/**
277
278
* return subset of found items (useful for paginate)
278
279
*
279
- * @param ContainerInterface $container
280
280
* @param Request $request
281
281
* @param int $page_size
282
282
* @param array $condition
@@ -285,8 +285,10 @@ public static function totalNum(ContainerInterface $container, $condition = []):
285
285
* @throws DependencyException
286
286
* @throws NotFoundException
287
287
*/
288
- public static function paginate (ContainerInterface $ container , Request $ request , $ page_size = self ::ITEMS_PER_PAGE , $ condition = [], $ order = []): array
288
+ public static function paginate (Request $ request , $ page_size = self ::ITEMS_PER_PAGE , $ condition = [], $ order = []): array
289
289
{
290
+ $ container = App::getInstance ()->getContainer ();
291
+
290
292
/** @var DebugBar $debugbar */
291
293
$ debugbar = $ container ->get ('debugbar ' );
292
294
@@ -300,8 +302,8 @@ public static function paginate(ContainerInterface $container, Request $request,
300
302
$ condition = [];
301
303
}
302
304
303
- $ stmt = static ::getModelBasicWhere ($ container , $ condition , $ order );
304
- $ out = static ::paginateByStatement ($ container , $ request , $ stmt , $ page_size );
305
+ $ stmt = static ::getModelBasicWhere ($ condition , $ order );
306
+ $ out = static ::paginateByStatement ($ request , $ stmt , $ page_size );
305
307
306
308
if (getenv ('DEBUG ' )) {
307
309
$ debugbar ['time ' ]->startMeasure ($ measure_key );
@@ -313,22 +315,21 @@ public static function paginate(ContainerInterface $container, Request $request,
313
315
/**
314
316
* return subset of found items (useful for paginate)
315
317
*
316
- * @param ContainerInterface $container
317
318
* @param Request $request
318
319
* @param Result $stmt
319
320
* @param int $page_size
320
321
* @return array
321
322
* @throws DependencyException
322
323
* @throws NotFoundException
323
324
*/
324
- public static function paginateByStatement (ContainerInterface $ container , Request $ request , Result $ stmt , $ page_size = self ::ITEMS_PER_PAGE ): array
325
+ public static function paginateByStatement (Request $ request , Result $ stmt , $ page_size = self ::ITEMS_PER_PAGE ): array
325
326
{
326
327
$ page = $ request ->get ('page ' ) ?? 0 ;
327
328
$ start = (int )$ page * $ page_size ;
328
329
329
330
$ total = (clone $ stmt )->count ();
330
331
331
- $ items = static ::hydrateStatementResult ($ container , $ stmt ->limit ($ page_size , $ start ));
332
+ $ items = static ::hydrateStatementResult ($ stmt ->limit ($ page_size , $ start ));
332
333
333
334
foreach ($ items as $ item ) {
334
335
static ::$ loadedObjects [static ::defaultTableName ()][$ item ->id ] = $ item ;
@@ -340,15 +341,16 @@ public static function paginateByStatement(ContainerInterface $container, Reques
340
341
/**
341
342
* finds elements
342
343
*
343
- * @param ContainerInterface $container
344
344
* @param array|string $condition
345
345
* @param array $order
346
346
* @return array
347
347
* @throws DependencyException
348
348
* @throws NotFoundException
349
349
*/
350
- public static function where (ContainerInterface $ container , $ condition , $ order = []): array
350
+ public static function where ($ condition , $ order = []): array
351
351
{
352
+ $ container = App::getInstance ()->getContainer ();
353
+
352
354
/** @var DebugBar $debugbar */
353
355
$ debugbar = $ container ->get ('debugbar ' );
354
356
@@ -358,7 +360,7 @@ public static function where(ContainerInterface $container, $condition, $order =
358
360
$ debugbar ['time ' ]->startMeasure ($ measure_key );
359
361
}
360
362
361
- $ items = static ::hydrateStatementResult ($ container , static ::getModelBasicWhere ($ container , $ condition , $ order ));
363
+ $ items = static ::hydrateStatementResult (static ::getModelBasicWhere ($ condition , $ order ));
362
364
363
365
foreach ($ items as $ item ) {
364
366
static ::$ loadedObjects [static ::defaultTableName ()][$ item ->id ] = $ item ;
@@ -464,13 +466,14 @@ public function reset(): BaseModel
464
466
/**
465
467
* loads model by id
466
468
*
467
- * @param ContainerInterface $container
468
469
* @param int $id
469
470
* @param bool $reset
470
471
* @return self
471
472
*/
472
- public static function load (ContainerInterface $ container , $ id , $ reset = false ): BaseModel
473
+ public static function load ($ id , $ reset = false ): BaseModel
473
474
{
475
+ $ container = App::getInstance ()->getContainer ();
476
+
474
477
/** @var DebugBar $debugbar */
475
478
$ debugbar = $ container ->get ('debugbar ' );
476
479
@@ -498,15 +501,16 @@ public static function load(ContainerInterface $container, $id, $reset = false):
498
501
/**
499
502
* loads multiple models by id
500
503
*
501
- * @param ContainerInterface $container
502
504
* @param array $ids
503
505
* @param bool $reset
504
506
* @return array
505
507
* @throws DependencyException
506
508
* @throws NotFoundException
507
509
*/
508
- public static function loadMultiple (ContainerInterface $ container , array $ ids , bool $ reset = false ): array
510
+ public static function loadMultiple (array $ ids , bool $ reset = false ): array
509
511
{
512
+ $ container = App::getInstance ()->getContainer ();
513
+
510
514
/** @var DebugBar $debugbar */
511
515
$ debugbar = $ container ->get ('debugbar ' );
512
516
@@ -532,7 +536,7 @@ public static function loadMultiple(ContainerInterface $container, array $ids, b
532
536
533
537
$ already_loaded = array_filter ($ already_loaded );
534
538
535
- $ out = (!empty ($ ids ) ? static ::loadMultipleByCondition ($ container , ['id ' => $ ids ], $ reset ) : []) +
539
+ $ out = (!empty ($ ids ) ? static ::loadMultipleByCondition (['id ' => $ ids ], $ reset ) : []) +
536
540
(!empty ($ already_loaded ) ? array_intersect_key (static ::$ loadedObjects [static ::defaultTableName ()], array_flip ($ already_loaded )) : []);
537
541
538
542
if (getenv ('DEBUG ' )) {
@@ -545,15 +549,16 @@ public static function loadMultiple(ContainerInterface $container, array $ids, b
545
549
/**
546
550
* loads model by condition
547
551
*
548
- * @param ContainerInterface $container
549
552
* @param array $condition
550
553
* @return self|null
551
554
* @throws BasicException
552
555
* @throws DependencyException
553
556
* @throws NotFoundException
554
557
*/
555
- public static function loadByCondition (ContainerInterface $ container , array $ condition ): ?BaseModel
558
+ public static function loadByCondition (array $ condition ): ?BaseModel
556
559
{
560
+ $ container = App::getInstance ()->getContainer ();
561
+
557
562
/** @var DebugBar $debugbar */
558
563
$ debugbar = $ container ->get ('debugbar ' );
559
564
@@ -563,7 +568,7 @@ public static function loadByCondition(ContainerInterface $container, array $con
563
568
$ debugbar ['time ' ]->startMeasure ($ measure_key );
564
569
}
565
570
566
- $ stmt = static ::getModelBasicWhere ($ container , $ condition );
571
+ $ stmt = static ::getModelBasicWhere ($ condition );
567
572
$ db_row = $ stmt ->limit (1 )->fetch ();
568
573
if (!$ db_row || !$ db_row ->id ) {
569
574
throw new BasicException ('Model not found ' );
@@ -581,15 +586,16 @@ public static function loadByCondition(ContainerInterface $container, array $con
581
586
/**
582
587
* loads multiple models by condition
583
588
*
584
- * @param ContainerInterface $container
585
589
* @param array $condition
586
590
* @param bool $reset
587
591
* @return array
588
592
* @throws DependencyException
589
593
* @throws NotFoundException
590
594
*/
591
- public static function loadMultipleByCondition (ContainerInterface $ container , array $ condition , bool $ reset = false ): array
595
+ public static function loadMultipleByCondition (array $ condition , bool $ reset = false ): array
592
596
{
597
+ $ container = App::getInstance ()->getContainer ();
598
+
593
599
/** @var DebugBar $debugbar */
594
600
$ debugbar = $ container ->get ('debugbar ' );
595
601
@@ -600,7 +606,7 @@ public static function loadMultipleByCondition(ContainerInterface $container, ar
600
606
}
601
607
602
608
$ ids = [];
603
- $ stmt = static ::getModelBasicWhere ($ container , $ condition );
609
+ $ stmt = static ::getModelBasicWhere ($ condition );
604
610
foreach ($ stmt ->fetchAll () as $ db_row ) {
605
611
$ ids [] = intval ($ db_row ->id );
606
612
/** @var Result $db_row */
@@ -620,14 +626,15 @@ public static function loadMultipleByCondition(ContainerInterface $container, ar
620
626
/**
621
627
* gets new empty model
622
628
*
623
- * @param ContainerInterface $container
624
629
* @param array $initial_data
625
630
* @return static
626
631
* @throws InvalidValueException
627
632
* @throws BasicException
628
633
*/
629
- public static function new (ContainerInterface $ container , $ initial_data = []): BaseModel
634
+ public static function new ($ initial_data = []): BaseModel
630
635
{
636
+ $ container = App::getInstance ()->getContainer ();
637
+
631
638
$ db_row = $ container ->get ('db ' )->createRow (static ::defaultTableName ());
632
639
$ db_row ->setData ($ initial_data );
633
640
return new static ($ container , $ db_row );
@@ -636,17 +643,16 @@ public static function new(ContainerInterface $container, $initial_data = []): B
636
643
/**
637
644
* loads model by field - value pair
638
645
*
639
- * @param ContainerInterface $container
640
646
* @param string $field
641
647
* @param mixed $value
642
648
* @return self
643
649
* @throws BasicException
644
650
* @throws DependencyException
645
651
* @throws NotFoundException
646
652
*/
647
- public static function loadBy (ContainerInterface $ container , string $ field , $ value ): BaseModel
653
+ public static function loadBy (string $ field , $ value ): BaseModel
648
654
{
649
- return static ::loadByCondition ($ container , [$ field => $ value ]);
655
+ return static ::loadByCondition ([$ field => $ value ]);
650
656
}
651
657
652
658
/**
0 commit comments