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\UserNotFoundException; |
19
|
|
|
use Ibonly\PotatoORM\EmptyDatabaseException; |
20
|
|
|
use Ibonly\PotatoORM\SaveUserExistException; |
21
|
|
|
use Ibonly\PotatoORM\ColumnNotExistExeption; |
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
|
|
|
return end($nameOfClass); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* getClassName() |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public static function getClassName() |
47
|
|
|
{ |
48
|
|
|
return self::pluralize(self::stripclassName()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* getTableName() |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public static function getTableName($connection) |
57
|
|
|
{ |
58
|
|
|
return DatabaseQuery::checkTableName(self::getClassName(), $connection); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* getALL() |
63
|
|
|
* Get all record from the database |
64
|
|
|
* |
65
|
|
|
* @return object |
66
|
|
|
*/ |
67
|
|
|
public function getALL($dbConnection = NULL) |
68
|
|
|
{ |
69
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
70
|
|
|
|
71
|
|
|
$sqlQuery = DatabaseQuery::selectAllQuery(self::getTableName($connection)); |
72
|
|
|
$query = $connection->prepare($sqlQuery); |
|
|
|
|
73
|
|
|
$query->execute(); |
74
|
|
|
if ( $query->rowCount() ) |
75
|
|
|
{ |
76
|
|
|
return new GetData($query->fetchAll($connection::FETCH_ASSOC)); |
77
|
|
|
} |
78
|
|
|
throw new EmptyDatabaseException(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* where($data, $condition) |
83
|
|
|
* Get data from database where $data = $condition |
84
|
|
|
* |
85
|
|
|
* @return object |
86
|
|
|
*/ |
87
|
|
|
public function where($data, $condition = NULL, $dbConnection = NULL) |
88
|
|
|
{ |
89
|
|
|
$databaseQuery = new DatabaseQuery(); |
90
|
|
|
$connection = $databaseQuery->checkConnection($dbConnection); |
91
|
|
|
|
92
|
|
|
$sqlQuery = $databaseQuery->selectQuery(self::getTableName($connection), $data, $condition, $connection); |
93
|
|
|
$query = $connection->prepare($sqlQuery); |
|
|
|
|
94
|
|
|
$query->execute(); |
95
|
|
|
if ( $query->rowCount() ) |
96
|
|
|
{ |
97
|
|
|
return new GetData($query->fetchAll($connection::FETCH_ASSOC)); |
98
|
|
|
} |
99
|
|
|
throw new UserNotFoundException(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* find($value) |
104
|
|
|
* Find data from database where id = $value |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public static function find($value, $dbConnection = NULL) |
109
|
|
|
{ |
110
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
111
|
|
|
|
112
|
|
|
$sqlQuery = DatabaseQuery::selectQuery(self::getTableName($connection), ['id' => $value], NULL, $connection); |
113
|
|
|
$query = $connection->prepare($sqlQuery); |
|
|
|
|
114
|
|
|
$query->execute(); |
115
|
|
|
if ( $query->rowCount() ) |
116
|
|
|
{ |
117
|
|
|
$found = new static; |
118
|
|
|
$found->id = $value; |
|
|
|
|
119
|
|
|
$found->data = $query->fetchAll($connection::FETCH_ASSOC); |
|
|
|
|
120
|
|
|
return $found; |
121
|
|
|
} |
122
|
|
|
throw new UserNotFoundException(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* save() |
127
|
|
|
* Insert data into database |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function save($dbConnection = NULL) |
132
|
|
|
{ |
133
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
134
|
|
|
|
135
|
|
|
$query = $this->insertQuery(self::getTableName($connection)); |
136
|
|
|
// $statement = $connection->prepare($query); |
|
|
|
|
137
|
|
|
// if( $statement->execute() ) |
|
|
|
|
138
|
|
|
// { |
139
|
|
|
// return true; |
140
|
|
|
// } |
141
|
|
|
// throw new SaveUserExistException(); |
|
|
|
|
142
|
|
|
return $query; |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* update() |
148
|
|
|
* Update details in database after ::find(2) |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function update($dbConnection = NULL) |
153
|
|
|
{ |
154
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
155
|
|
|
|
156
|
|
|
$updateQuery = $this->updateQuery(self::getTableName($connection)); |
157
|
|
|
$statement = $connection->prepare($updateQuery); |
|
|
|
|
158
|
|
|
if( $statement->execute() ) |
159
|
|
|
{ |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
throw new SaveUserExistException(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* destroy($value) |
167
|
|
|
* Delete data from database |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function destroy($value, $dbConnection = NULL) |
172
|
|
|
{ |
173
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
174
|
|
|
|
175
|
|
|
$query = $connection->prepare('DELETE FROM ' . self::getTableName($connection) . ' WHERE id = '.$value); |
|
|
|
|
176
|
|
|
$query->execute(); |
177
|
|
|
$check = $query->rowCount(); |
178
|
|
|
if ($check) |
179
|
|
|
{ |
180
|
|
|
return true; |
181
|
|
|
} |
182
|
|
|
throw new UserNotFoundException; |
183
|
|
|
} |
184
|
|
|
} |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.