1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PotatoORM manages the persistence of database CRUD operations. |
4
|
|
|
* |
5
|
|
|
* @package Ibonly\PotatoORM\Model |
6
|
|
|
* @author Ibraheem ADENIYI <[email protected]> |
7
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Ibonly\PotatoORM; |
11
|
|
|
|
12
|
|
|
use PDO; |
13
|
|
|
use Exception; |
14
|
|
|
use PDOException; |
15
|
|
|
use Ibonly\PotatoORM\GetData; |
16
|
|
|
use Ibonly\PotatoORM\DatabaseQuery; |
17
|
|
|
use Ibonly\PotatoORM\ModelInterface; |
18
|
|
|
use Ibonly\PotatoORM\DataNotFoundException; |
19
|
|
|
use Ibonly\PotatoORM\EmptyDatabaseException; |
20
|
|
|
use Ibonly\PotatoORM\ColumnNotExistExeption; |
21
|
|
|
use Ibonly\PotatoORM\DataAlreadyExistException; |
22
|
|
|
use Ibonly\PotatoORM\InvalidConnectionException; |
23
|
|
|
|
24
|
|
|
class Model extends DatabaseQuery implements ModelInterface |
25
|
|
|
{ |
26
|
|
|
//Inject the inflector trait |
27
|
|
|
use Inflector; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* stripclassName() |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public static function stripclassName() |
35
|
|
|
{ |
36
|
|
|
$className = strtolower(get_called_class()); |
37
|
|
|
$nameOfClass = explode("\\", $className); |
38
|
|
|
|
39
|
|
|
return end($nameOfClass); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the table name if defined in the model |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function tableName() |
48
|
|
|
{ |
49
|
|
|
if(isset($this->table)) { |
50
|
|
|
return $this->table; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the fields to be fillables defined in the model |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function fields() |
61
|
|
|
{ |
62
|
|
|
if (isset($this->fillables)) { |
63
|
|
|
if (sizeof($this->fillables) > 0) { |
64
|
|
|
return implode(", ", $this->fillables); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
return '*'; |
67
|
|
|
} |
68
|
|
|
return '*'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* getClassName() |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getClassName() |
77
|
|
|
{ |
78
|
|
|
if ($this->tableName() === null) { |
79
|
|
|
return self::pluralize(self::stripclassName()); |
80
|
|
|
} else { |
81
|
|
|
return $this->tableName(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* getTableName() |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getTableName($connection) |
91
|
|
|
{ |
92
|
|
|
return DatabaseQuery::checkTableName($this->getClassName(), $connection); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* getALL() |
97
|
|
|
* Get all record from the database |
98
|
|
|
* |
99
|
|
|
* @return object |
100
|
|
|
*/ |
101
|
|
|
public function getALL($dbConnection = NULL) |
102
|
|
|
{ |
103
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
104
|
|
|
|
105
|
|
|
$sqlQuery = DatabaseQuery::selectAllQuery(self::getTableName($connection), self::fields()); |
106
|
|
|
$query = $connection->prepare($sqlQuery); |
|
|
|
|
107
|
|
|
$query->execute(); |
108
|
|
|
if ( $query->rowCount() ) |
109
|
|
|
{ |
110
|
|
|
return new GetData($query->fetchAll($connection::FETCH_ASSOC)); |
111
|
|
|
} |
112
|
|
|
throw new EmptyDatabaseException(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* where($data, $condition) |
117
|
|
|
* Get data from database where $data = $condition |
118
|
|
|
* |
119
|
|
|
* @return object |
120
|
|
|
*/ |
121
|
|
|
public function where($data, $condition = NULL, $dbConnection = NULL) |
122
|
|
|
{ |
123
|
|
|
$databaseQuery = new DatabaseQuery(); |
124
|
|
|
$connection = $databaseQuery->checkConnection($dbConnection); |
125
|
|
|
|
126
|
|
|
$sqlQuery = $databaseQuery->selectQuery(self::getTableName($connection), $data, $condition, $connection); |
127
|
|
|
$query = $connection->prepare($sqlQuery); |
|
|
|
|
128
|
|
|
$query->execute(); |
129
|
|
|
if ( $query->rowCount() ) |
130
|
|
|
{ |
131
|
|
|
return new GetData($query->fetchAll($connection::FETCH_ASSOC)); |
132
|
|
|
} |
133
|
|
|
throw new DataNotFoundException(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* find($value) |
138
|
|
|
* Find data from database where id = $value |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public static function find($value, $dbConnection = NULL) |
143
|
|
|
{ |
144
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
145
|
|
|
|
146
|
|
|
$sqlQuery = DatabaseQuery::selectQuery(self::getTableName($connection), ['id' => $value], NULL, $connection); |
147
|
|
|
$query = $connection->prepare($sqlQuery); |
|
|
|
|
148
|
|
|
$query->execute(); |
149
|
|
|
if ( $query->rowCount() ) |
150
|
|
|
{ |
151
|
|
|
$found = new static; |
152
|
|
|
$found->id = $value; |
|
|
|
|
153
|
|
|
$found->data = $query->fetchAll($connection::FETCH_ASSOC); |
|
|
|
|
154
|
|
|
return $found; |
155
|
|
|
} |
156
|
|
|
throw new DataNotFoundException(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* save() |
161
|
|
|
* Insert data into database |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function save($dbConnection = NULL) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
168
|
|
|
|
169
|
|
|
$query = $this->insertQuery(self::getTableName($connection)); |
170
|
|
|
$statement = $connection->prepare($query); |
|
|
|
|
171
|
|
|
if( $statement->execute() ) |
172
|
|
|
{ |
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
throw new DataAlreadyExistException(); |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* update() |
181
|
|
|
* Update details in database after ::find(2) |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function update($dbConnection = NULL) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
188
|
|
|
|
189
|
|
|
$updateQuery = $this->updateQuery(self::getTableName($connection)); |
190
|
|
|
$statement = $connection->prepare($updateQuery); |
|
|
|
|
191
|
|
|
if( $statement->execute() ) |
192
|
|
|
{ |
193
|
|
|
return true; |
194
|
|
|
} |
195
|
|
|
throw new DataAlreadyExistException(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* destroy($value) |
200
|
|
|
* Delete data from database |
201
|
|
|
* |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
public function destroy($value, $dbConnection = NULL) |
205
|
|
|
{ |
206
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
207
|
|
|
|
208
|
|
|
$query = $connection->prepare('DELETE FROM ' . self::getTableName($connection) . ' WHERE id = '.$value); |
|
|
|
|
209
|
|
|
$query->execute(); |
210
|
|
|
$check = $query->rowCount(); |
211
|
|
|
if ($check) |
212
|
|
|
{ |
213
|
|
|
return true; |
214
|
|
|
} |
215
|
|
|
throw new DataNotFoundException; |
216
|
|
|
} |
217
|
|
|
} |
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: