1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vundi\Potato; |
4
|
|
|
|
5
|
|
|
use Vundi\Potato\Exceptions\IDShouldBeNumber; |
6
|
|
|
|
7
|
|
|
class Model |
8
|
|
|
{ |
9
|
|
|
private static $db; |
10
|
|
|
protected static $entity_table; |
11
|
|
|
protected static $entity_class; |
12
|
|
|
protected $db_fields = []; |
13
|
|
|
public static $ID; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
self::$db = new Database(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function __set($key, $value) |
21
|
|
|
{ |
22
|
|
|
$this->db_fields[$key] = $value; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function save() |
26
|
|
|
{ |
27
|
|
|
//var_dump($this->db_fields); |
|
|
|
|
28
|
|
|
$s = new static(); |
29
|
|
|
// foreach ($s::$db_fields as $key) { |
|
|
|
|
30
|
|
|
// $data[$key] = $this->$key; |
|
|
|
|
31
|
|
|
// } |
32
|
|
|
|
33
|
|
|
return self::$db->insert($s::$entity_table, $this->db_fields); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function update() |
37
|
|
|
{ |
38
|
|
|
$s = new static(); |
39
|
|
|
|
40
|
|
|
$where = "id = {$s::$ID}"; |
41
|
|
|
|
42
|
|
|
self::$db->update($s::$entity_table, $this->db_fields, $where); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function remove($id) |
46
|
|
|
{ |
47
|
|
|
if (is_int($id)) { |
48
|
|
|
$where = "id = {$id}"; |
49
|
|
|
$s = new static(); |
50
|
|
|
self::$db->delete($s::$entity_table, $where); |
51
|
|
|
} else { |
52
|
|
|
throw new IDShouldBeNumber('Pass in an ID as the parameter, ID has to be a number', 1); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function find($id) |
57
|
|
|
{ |
58
|
|
|
if (is_int($id)) { |
59
|
|
|
$s = new static(); |
60
|
|
|
//comment |
61
|
|
|
$s::$ID = $id; |
62
|
|
|
|
63
|
|
|
$where = "id = {$id}"; |
64
|
|
|
self::$db->select($s::$entity_table, $where); |
65
|
|
|
|
66
|
|
|
return self::$db->singleObject($s::$entity_class); |
67
|
|
|
} else { |
68
|
|
|
throw new IDShouldBeNumber('Find only takes a number as a parameter', 1); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function findAll() |
73
|
|
|
{ |
74
|
|
|
$s = new static(); |
75
|
|
|
|
76
|
|
|
self::$db->select($s::$entity_table); |
77
|
|
|
|
78
|
|
|
return self::$db->objectSet(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.