add static method to get inherited class querybuilder from model, add isset method in model
This commit is contained in:
@@ -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
|
||||
{
|
||||
if (isset($this->values[$name])) {
|
||||
|
||||
@@ -91,7 +91,21 @@ test("querybuilder", function() {
|
||||
|
||||
$qb->reset();
|
||||
$rows = $qb->whereNotNull("col1")->get();
|
||||
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(){
|
||||
@@ -119,7 +133,18 @@ test("group by", function(){
|
||||
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() {
|
||||
$qb = new QueryBuilder("test","id");
|
||||
|
||||
Reference in New Issue
Block a user