1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vundi\Potato; |
4
|
|
|
|
5
|
|
|
use Vundi\Potato\Exceptions\IDShouldBeNumber; |
6
|
|
|
|
7
|
|
|
class Model |
8
|
|
|
{ |
9
|
|
|
private static $db; |
10
|
|
|
public $db_fields = []; |
11
|
|
|
public static $ID; |
12
|
|
|
protected static $child_class; |
13
|
|
|
|
14
|
|
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
self::$db = new Database(); |
17
|
|
|
self::$child_class = get_called_class(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Will make it possible to assign key value pairs dynamically |
22
|
|
|
* in the child class |
23
|
|
|
*/ |
24
|
|
|
public function __set($key, $value) |
25
|
|
|
{ |
26
|
|
|
$this->db_fields[$key] = $value; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Save a record in the table |
31
|
|
|
* calls the insert method in the Database class |
32
|
|
|
*/ |
33
|
|
|
public function save() |
34
|
|
|
{ |
35
|
|
|
$s = new static(); |
36
|
|
|
|
37
|
|
|
return self::$db->insert($s::$entity_table, $this->db_fields); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Update a record in the table |
42
|
|
|
*/ |
43
|
|
|
public function update() |
44
|
|
|
{ |
45
|
|
|
$s = new static(); |
46
|
|
|
|
47
|
|
|
$where = "id = {$s::$ID}"; |
48
|
|
|
|
49
|
|
|
return self::$db->update($s::$entity_table, $this->db_fields, $where); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Remove a record from the table with the specified ID |
54
|
|
|
* @param in $id ID of record you want to remove |
55
|
|
|
*/ |
56
|
|
|
public static function remove($id) |
57
|
|
|
{ |
58
|
|
|
$s = new static(); |
59
|
|
|
|
60
|
|
|
if (is_int($id)) { |
61
|
|
|
$where = "id = {$id}"; |
62
|
|
|
self::$db->delete($s::$entity_table, $where); |
63
|
|
|
} else { |
64
|
|
|
throw new IDShouldBeNumber('Pass in an ID as the parameter, ID has to be a number', 1); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param int $id ID of the record to be retrieved |
70
|
|
|
* @return object Instance of the Class |
71
|
|
|
*/ |
72
|
|
|
public static function find($id) |
73
|
|
|
{ |
74
|
|
|
// var_dump(get_called_class()); |
|
|
|
|
75
|
|
|
// die(); |
76
|
|
|
$s = new static(); |
77
|
|
|
|
78
|
|
|
if (is_int($id)) { |
79
|
|
|
//Set the ID of the class instance returned to $id since during update we shall update the |
80
|
|
|
//record with ID that matches the id passed during find |
81
|
|
|
$s::$ID = $id; |
82
|
|
|
|
83
|
|
|
$where = "id = {$id}"; |
84
|
|
|
self::$db->select($s::$entity_table, $where); |
85
|
|
|
|
86
|
|
|
return self::$db->singleObject(self::$child_class); |
87
|
|
|
} else { |
88
|
|
|
throw new IDShouldBeNumber('Find only takes a number as a parameter', 1); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function findWhere($conditions = array(), $fields = '*', $order = '', $limit = null, $offset = '') |
93
|
|
|
{ |
94
|
|
|
$s = new static(); |
95
|
|
|
$where = self::parseWhereConditions($conditions); |
96
|
|
|
self::$db->select($s::$entity_table, $where, $fields, $order, $limit, $offset); |
97
|
|
|
|
98
|
|
|
return self::$db->objectSet(self::$child_class); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private static function parseWhereConditions($conditions, $logicOp = '&&') { |
102
|
|
|
foreach ($conditions as $key => $value) { |
103
|
|
|
if (is_array($value)) { // support aggregating conditions with another boolean operator |
104
|
|
|
$where[] = '(' . self::parseWhereConditions($value, $key) . ')'; |
|
|
|
|
105
|
|
|
} elseif (is_string($value)) { |
106
|
|
|
$where[] = $key.' ="'.addslashes($value).'"'; |
|
|
|
|
107
|
|
|
} else { |
108
|
|
|
$where[] = $key.' = '.$value; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
if (!is_string($logicOp)) // caller sent us an indexed array. |
112
|
|
|
// they probably only have 1 condition, but better safe than sorry |
113
|
|
|
$logicOp = '&&'; |
114
|
|
|
return join(" $logicOp ", $where); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Finds all records in the table |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public static function findAll() |
122
|
|
|
{ |
123
|
|
|
$s = new static(); |
124
|
|
|
|
125
|
|
|
self::$db->select($s::$entity_table); |
126
|
|
|
|
127
|
|
|
return self::$db->objectSet(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.