add static method to get inherited class querybuilder from model, add isset method in model

This commit is contained in:
Gregory Letellier
2023-11-13 09:01:51 +01:00
parent 523d307f81
commit bdb0a88e58
2 changed files with 41 additions and 1 deletions

View File

@@ -121,6 +121,21 @@ class Model
} }
} }
public function __isset($name): bool
{
if (method_exists($this, $name)) {
return true;
} else {
return isset($this->values[$name]);
}
}
public static function q(): QueryBuilder
{
$ist = new static();
return $ist->newQueryBuilder();
}
public function __get($name): mixed public function __get($name): mixed
{ {
if (isset($this->values[$name])) { if (isset($this->values[$name])) {

View File

@@ -94,6 +94,20 @@ test("querybuilder", function() {
expect(count($rows))->toBe(2); expect(count($rows))->toBe(2);
}); });
test("extending model",function()
{
$class = "Test";
$code = "class $class extends \Kletellier\PdoWrapper\Model {}";
eval($code);
$qbs = Test::q();
expect($qbs)->toBeInstanceOf(QueryBuilder::class);
$row = $qbs->find(1);
expect($row->id)->toBe(1);
expect($row->col1)->toBe("test");
});
test("order by", function(){ test("order by", function(){
$qb = new QueryBuilder("test"); $qb = new QueryBuilder("test");
$rows = $qb->orderBy("id","DESC")->get(); $rows = $qb->orderBy("id","DESC")->get();
@@ -119,7 +133,18 @@ test("group by", function(){
expect($rows->first()->Nb)->toBe(1); expect($rows->first()->Nb)->toBe(1);
}); });
test("isset",function()
{
$q = new QueryBuilder("test");
$row = $q->where("id",1)->get()->first();
expect($row->id)->toBe(1);
expect($row->col1)->toBe("test");
expect(isset($row->col1))->toBe(true);
expect(isset($row->col2))->toBe(false);
expect(isset($row->find))->toBe(true);
});
test("update row", function() { test("update row", function() {
$qb = new QueryBuilder("test","id"); $qb = new QueryBuilder("test","id");