-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseModel.php
72 lines (57 loc) · 1.69 KB
/
BaseModel.php
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
<?php
namespace Core\Database;
use Core\Database\QueryBuilder;
abstract class BaseModel {
/**
* Get the original table name of the called model
* @return string
*/
public static function getTableName(){
$target = get_called_class();
$target = str_replace('App\\Models\\', '', $target);
$target = strtolower($target).'s';
return $target;
}
/**
* Find a matching-id record within the attached model
* @return array
*/
public static function find($id) {
$table = self::getTableName();
$result = QueryBuilder::select(table: $table, where: 'id', value: $id);
return $result;
}
/**
* Get a set of records within the attached model
* @return array
*/
public static function get() {
$table = self::getTableName();
$result = QueryBuilder::select(table: $table);
return $result;
}
/**
* Create a new record using a given data
* @return void
*/
public static function create($data){
$table = self::getTableName();
$result = QueryBuilder::insert(table: $table, columns: array_keys($data), values: array_values($data));
}
/**
* delete a given matching-id record
* @return void
*/
public static function delete($id){
$table = self::getTableName();
$result = QueryBuilder::delete(table: $table, column: 'id', value: $id);
}
/**
* update a given matching-id record
* @return void
*/
public static function update($id, $data){
$table = self::getTableName();
$result = QueryBuilder::update(table: $table, where: 'id', value: $id, data: $data);
}
}