1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file Entity.php |
4
|
|
|
* This class is a model that can be inherited by other classes |
5
|
|
|
* and used to persist and retrieve objects from a database |
6
|
|
|
* @package PotatoORM |
7
|
|
|
* @author andrew <[email protected]> |
8
|
|
|
* @license MIT => https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PotatoORM; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @category Class |
15
|
|
|
* @package PotatoORM |
16
|
|
|
*/ |
17
|
|
|
class Entity |
18
|
|
|
{ |
19
|
|
|
private $db; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Instantiates a Database object |
23
|
|
|
*/ |
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->db = new Database(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Adds a record to the database and returns the last inserted ID |
31
|
|
|
* @return int |
32
|
|
|
*/ |
33
|
|
|
public function add() |
34
|
|
|
{ |
35
|
|
|
foreach ($this->db_fields as $key) { |
|
|
|
|
36
|
|
|
$data[$key] = $this->$key; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->db->insert($this->entity_table, $data); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Updates an existing record in the database |
44
|
|
|
*/ |
45
|
|
|
public function update() |
46
|
|
|
{ |
47
|
|
|
foreach ($this->db_fields as $key) { |
48
|
|
|
if (!is_null($this->$key)) { |
49
|
|
|
$data[$key] = $this->$key; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$where = ""; |
54
|
|
|
foreach ($this->primary_keys as $key) { |
|
|
|
|
55
|
|
|
$where .= " $key = " . $this->$key . " &&"; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$where = rtrim($where, "&"); |
59
|
|
|
$this->db->update($this->entity_table, $data, $where); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Deletes a record from the database |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
|
|
public function remove() |
67
|
|
|
{ |
68
|
|
|
$where = ""; |
69
|
|
|
foreach ($this->primary_keys as $key) { |
70
|
|
|
$where .= " $key = " . $this->$key . " &&"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$where = rtrim($where, "&"); |
74
|
|
|
$this->db->delete($this->entity_table, $where); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retrieves a record from the database based on its ID |
79
|
|
|
* and returns it in the form of the corresponding object |
80
|
|
|
* @return object |
81
|
|
|
*/ |
82
|
|
|
public function find($id) |
83
|
|
|
{ |
84
|
|
|
$this->db->select($this->entity_table, " id = $id"); |
85
|
|
|
|
86
|
|
|
return $this->db->singleObject(get_called_class()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieves all records from the corresponding table in the |
91
|
|
|
* database and returns them as an array of objects |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function findAll() |
95
|
|
|
{ |
96
|
|
|
$this->db->select($this->entity_table); |
97
|
|
|
|
98
|
|
|
return $this->db->objectSet(get_called_class()); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: