diff --git a/src/Model.php b/src/Model.php index 2eb64b8..734f12e 100644 --- a/src/Model.php +++ b/src/Model.php @@ -31,6 +31,11 @@ class Model return $this; } + public function getValues(): array + { + return $this->values; + } + public function getPk(): string { return $this->pk; @@ -61,6 +66,19 @@ class Model return $this; } + public function find(mixed $key): bool + { + $res = false; + $qb = $this->newQueryBuilder(); + $find = $qb->find($key); + if($find!==null) + { + $this->fillData($find->getValues()); + $res = true; + } + return $res; + } + public function save(): bool { $ret = false; @@ -121,5 +139,6 @@ class Model $table = strtolower($classNameWithoutNamespace); return $table; - } + } + } diff --git a/tests/DbTest.php b/tests/DbTest.php index b2f1bc8..375e239 100644 --- a/tests/DbTest.php +++ b/tests/DbTest.php @@ -35,6 +35,23 @@ test("insert row",function () { expect($ret2)->toBe(true); }); +test("model find", function() { + $row = new Model(); + $row->setTable("test"); + + $res = $row->find(1); + expect($res)->toBe(true); + expect($row->col1)->toBe("test"); + + $row->col1 = "test2"; + $res = $row->save(); + expect($res)->toBe(true); + + $row->col1 = "test"; + $res = $row->save(); + expect($res)->toBe(true); +}); + test("querybuilder", function() { $qb = new QueryBuilder("test"); $row = $qb->find(1);