|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Opeyemiabiodun\PotatoORM\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\Connection; |
|
7
|
|
|
use Opeyemiabiodun\PotatoORM\Connections\MySqlConnection; |
|
8
|
|
|
|
|
9
|
|
|
trait Model |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The model's attributes. |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $_attributes = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The model's database connection. |
|
19
|
|
|
* @var Opeyemiabiodun\PotatoORM\Connections\Connection |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $_connection; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The primary key of the model's database table. |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $_primaryKey; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The model's database table. |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_table; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The model's constructor method. |
|
37
|
|
|
* @param Connection|null $connection An Opeyemiabiodun\PotatoORM\Connections\Connection instance or null |
|
38
|
|
|
* @param string $table The name of the model's table in the database |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Connection $connection = null, $table = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if (is_null($connection)) { |
|
43
|
|
|
$this->setConnection(new MySqlConnection()); |
|
44
|
|
|
} else { |
|
45
|
|
|
$this->setConnection($connection); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (is_null($table)) { |
|
49
|
|
|
$this->setTable(get_class($this).'-table'); |
|
50
|
|
|
} else { |
|
51
|
|
|
$this->setTable($table); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The getter method for the model's properties. |
|
57
|
|
|
* @param string $property The particular property |
|
58
|
|
|
* @return int|float|string|bool The value of the property |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __get($property) |
|
61
|
|
|
{ |
|
62
|
|
|
if (array_key_exists($property, $_attributes)) |
|
63
|
|
|
{ |
|
64
|
|
|
return $_attributes[$property]; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The setter method for the model's properties. |
|
70
|
|
|
* @param string $property The particular property |
|
71
|
|
|
* @param int|float|string|bool $value The value of the property |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __set($property, $value) |
|
74
|
|
|
{ |
|
75
|
|
|
if (! is_scalar($value)) |
|
76
|
|
|
{ |
|
77
|
|
|
throw new Exception("Error Processing Request", 1); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (array_key_exists($property, $_attributes)) |
|
81
|
|
|
{ |
|
82
|
|
|
$_attributes[$property] = $value; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Deletes a specified instance of the model in the database. |
|
88
|
|
|
* @param int $number Specifies which model instance to delete; the 1st, 2nd, 3rd, ..... |
|
89
|
|
|
* @return bool Returns boolean true if the instance was successfully deleted or else it returns false. |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function destroy($number) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->_connection->deleteRecord($this->_table, $number - 1); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Finds a specified instance of the model in the database. |
|
98
|
|
|
* @param int $number Specifies which model instance to find; the 1st, 2nd, 3rd, ..... |
|
99
|
|
|
* @return array Returns the particular instance of the model. |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function find($number) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($number <= 0) { |
|
104
|
|
|
throw new Exception("Error Processing Request", 1); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->_connection->findRecord($this->_table, $number - 1); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns all instances of the model in the database. |
|
112
|
|
|
* @return array All instances of the model in the database. |
|
113
|
|
|
*/ |
|
114
|
|
|
public static function getAll() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->_connection->getAllRecords($this->_table); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Checks the attributes of the model to ensure they are not all null. |
|
121
|
|
|
* @return bool true if at least one of the models's attributes is not null else false. |
|
122
|
|
|
*/ |
|
123
|
|
|
private function hasAttributes() |
|
124
|
|
|
{ |
|
125
|
|
|
$hasAttributes = false; |
|
126
|
|
|
|
|
127
|
|
|
foreach ($this->_attributes as $key => $value) { |
|
128
|
|
|
if (! is_null($value)) { |
|
129
|
|
|
$hasAttributes = true; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $hasAttributes; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Saves or updates an instance of the model in the database. |
|
138
|
|
|
* @return bool Returns true if the operation was successfully else returns false. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function save() |
|
141
|
|
|
{ |
|
142
|
|
|
if (is_null($this->_connection)) { |
|
143
|
|
|
throw new Exception("Error Processing Request", 1); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if (is_null($this->_table)) { |
|
147
|
|
|
throw new Exception("Error Processing Request", 1); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if (! $this->hasAttributes()) { |
|
151
|
|
|
throw new Exception("Error Processing Request", 1); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
if (empty($this->_connection->findRecord($this->_table, $this->_attributes)) { |
|
|
|
|
|
|
155
|
|
|
return $this->_connection->createRecord($this->_table, $this->_attributes); |
|
156
|
|
|
} else { |
|
157
|
|
|
return $this->_connection->updateRecord($this->_table, $this->_attributes); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Sets the model's connection. |
|
163
|
|
|
* @param Connection $connection An instance of Opeyemiabiodun\PotatoORM\Connections\Connection. |
|
164
|
|
|
*/ |
|
165
|
|
|
protected static function setConnection(Connection $connection) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->_connection = $connection; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Sets the model's table. |
|
172
|
|
|
* @param string $table An existing table in the database. |
|
173
|
|
|
*/ |
|
174
|
|
|
protected static function setTable($table) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->_table = $table; |
|
177
|
|
|
|
|
178
|
|
|
$columns = $this->_connection->getColumns($table); |
|
179
|
|
|
for ($i=0; $i < count($columns); $i++) { |
|
180
|
|
|
array_push($this->_attributes, $columns[i][key($columns[i])]); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$this->_primaryKey = $this->_connection->getPrimaryKey($table); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
|