-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
119 lines (101 loc) · 1.92 KB
/
Model.php
File metadata and controls
119 lines (101 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Silver\Database;
class Model extends QueryObject
{
public static function query(): object
{
return Query::select()
->from(static::class)
->setFetchStyle(static::class);
}
public static function where($column, string $op = null, $value = null): object
{
return static::query()
->where($column, $op, $value);
}
public static function find(int $id): object
{
return static::where(static::primaryKey(), $id)
->first();
}
/**
* @return mixed
*/
public function getFilterable()
{
return $this->filterable;
}
/**
* @return mixed
*/
public function getIncludable()
{
return $this->includable;
}
/**
* @return mixed
*/
public function getSearchable()
{
return $this->searchable;
}
/**
* @return mixed
*/
public function getHidden()
{
return $this->hidden;
}
/**
* @return mixed
*/
public function getFillable()
{
return $this->fillable;
}
/**
* @return mixed
*/
public function getSelectable()
{
return $this->selectable;
}
public static function all(): array
{
return static::query()->all();
}
public static function create(array $data): object
{
Query::insert(static::class, $data)->execute();
return static::find(Query::lastInsertId());
}
public function delete(): void
{
Query::delete()
->from(static::class)
->where(static::primaryKey(), $this->id)
->execute();
}
public function save(): object
{
$id = static::primaryKey();
if (isset($this->$id)) {
$dirty = $this->dirtyData();
if (count($dirty) > 0) {
$q = Query::update(static::class)->where(static::primaryKey(), $this->$id);
foreach ($dirty as $key => $val) {
$q->set($key, $val);
}
$q->execute();
}
return $this;
} else {
Query::insert(static::class, $this->data())->execute();
Query::select()
->from(static::class)
->where(static::primaryKey(), Query::lastInsertId())
->first($this);
return $this;
}
}
}