File tree Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Original file line number Diff line number Diff line change 15
15
use Exception ;
16
16
use Framework \Cache \Cache ;
17
17
use Framework \Database \Database ;
18
+ use Framework \Database \Manipulation \Traits \Where ;
18
19
use Framework \Language \Language ;
19
20
use Framework \Pagination \Pager ;
20
21
use Framework \Validation \Debug \ValidationCollector ;
@@ -401,23 +402,28 @@ protected function getDatabaseToWrite() : Database
401
402
}
402
403
403
404
/**
404
- * A basic function to count all rows in the table.
405
+ * A basic function to count rows in the table.
406
+ *
407
+ * @param array<array<mixed>> $where Array in this format: `[['id', '=', 25]]`
408
+ *
409
+ * @see Where
405
410
*
406
411
* @return int
407
412
*/
408
- public function count () : int
413
+ public function count (array $ where = [] ) : int
409
414
{
410
- $ result = $ this ->getDatabaseToRead ()
415
+ $ select = $ this ->getDatabaseToRead ()
411
416
->select ()
412
417
->expressions ([
413
418
'count ' => static function () : string {
414
419
return 'COUNT(*) ' ;
415
420
},
416
421
])
417
- ->from ($ this ->getTable ())
418
- ->run ()
419
- ->fetch ();
420
- return $ result ->count ; // @phpstan-ignore-line
422
+ ->from ($ this ->getTable ());
423
+ foreach ($ where as $ args ) {
424
+ $ select ->where (...$ args );
425
+ }
426
+ return $ select ->run ()->fetch ()->count ; // @phpstan-ignore-line
421
427
}
422
428
423
429
/**
Original file line number Diff line number Diff line change @@ -397,6 +397,13 @@ public function testDeleteByWithCall() : void
397
397
public function testCount () : void
398
398
{
399
399
self ::assertSame (2 , $ this ->model ->count ());
400
+ self ::assertSame (1 , $ this ->model ->count ([
401
+ ['id ' , '= ' , 1 ],
402
+ ]));
403
+ self ::assertSame (2 , $ this ->model ->count ([
404
+ ['id ' , 'is not null ' ],
405
+ ['id ' , '< ' , 3 ],
406
+ ]));
400
407
}
401
408
402
409
public function testPaginatedItems () : void
You can’t perform that action at this time.
0 commit comments