File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 9
9
*/
10
10
namespace Framework \MVC ;
11
11
12
+ use Closure ;
12
13
use DateTime ;
13
14
use DateTimeZone ;
14
15
use Exception ;
@@ -482,6 +483,28 @@ public function getPager() : Pager
482
483
return $ this ->pager ;
483
484
}
484
485
486
+ /**
487
+ * Find a row by column name and value.
488
+ *
489
+ * @param string $column
490
+ * @param Closure|float|int|string|null $value
491
+ *
492
+ * @return array<string,float|int|string|null>|Entity|stdClass|null
493
+ */
494
+ public function findBy (
495
+ string $ column ,
496
+ Closure | float | int | string | null $ value
497
+ ) : array | Entity | stdClass | null {
498
+ $ data = $ this ->getDatabaseToRead ()
499
+ ->select ()
500
+ ->from ($ this ->getTable ())
501
+ ->whereEqual ($ column , $ value )
502
+ ->limit (1 )
503
+ ->run ()
504
+ ->fetchArray ();
505
+ return $ data ? $ this ->makeEntity ($ data ) : null ;
506
+ }
507
+
485
508
/**
486
509
* Find a row based on Primary Key.
487
510
*
Original file line number Diff line number Diff line change @@ -27,6 +27,13 @@ protected function setUp() : void
27
27
$ this ->model = new ModelMock ();
28
28
}
29
29
30
+ public function testFindBy () : void
31
+ {
32
+ self ::assertIsObject ($ this ->model ->findBy ('id ' , 1 ));
33
+ self ::assertNull ($ this ->model ->findBy ('id ' , 1000 ));
34
+ self ::assertIsObject ($ this ->model ->findBy ('data ' , 'foo ' ));
35
+ }
36
+
30
37
public function testFind () : void
31
38
{
32
39
self ::assertIsObject ($ this ->model ->find (1 ));
You can’t perform that action at this time.
0 commit comments