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\DataNotFoundException; |
17
|
|
|
use Ibonly\PotatoORM\EmptyDatabaseException; |
18
|
|
|
use Ibonly\PotatoORM\ColumnNotExistExeption; |
19
|
|
|
use Ibonly\PotatoORM\DataAlreadyExistException; |
20
|
|
|
use Ibonly\PotatoORM\InvalidConnectionException; |
21
|
|
|
|
22
|
|
|
class Model extends Relationships implements ModelInterface, RelationshipsInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* getALL() |
26
|
|
|
* Get all record from the database |
27
|
|
|
* |
28
|
|
|
* @return object |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
public function getAll($dbConnection = NULL) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
33
|
|
|
|
34
|
|
|
$sqlQuery = self::whereClause(); |
35
|
|
|
$query = $connection->prepare($sqlQuery); |
|
|
|
|
36
|
|
|
$query->execute(); |
37
|
|
|
if ($query->rowCount()) { |
38
|
|
|
return new GetData($query->fetchAll($connection::FETCH_ASSOC)); |
39
|
|
|
} |
40
|
|
|
throw new EmptyDatabaseException(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* where($data, $condition) |
45
|
|
|
* Get data from database where $data = $condition |
46
|
|
|
* |
47
|
|
|
* @return object |
48
|
|
|
*/ |
49
|
|
|
public function where($data, $condition = NULL, $dbConnection = NULL) |
50
|
|
|
{ |
51
|
|
|
$databaseQuery = new DatabaseQuery(); |
52
|
|
|
$connection = $databaseQuery->checkConnection($dbConnection); |
53
|
|
|
|
54
|
|
|
$sqlQuery = self::whereClause($data, $condition, $connection); |
55
|
|
|
$query = $connection->prepare($sqlQuery); |
|
|
|
|
56
|
|
|
$query->execute(); |
57
|
|
|
if ($query->rowCount()) { |
58
|
|
|
return new GetData($query->fetchAll($connection::FETCH_ASSOC)); |
59
|
|
|
} |
60
|
|
|
throw new DataNotFoundException(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* find($value) |
65
|
|
|
* Find data from database where id = $value |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public static function find($value, $dbConnection = NULL) |
70
|
|
|
{ |
71
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
72
|
|
|
|
73
|
|
|
$sqlQuery = DatabaseQuery::selectQuery(self::getTableName($connection), self::fields(), ['id' => $value], NULL, $connection); |
74
|
|
|
$query = $connection->prepare($sqlQuery); |
|
|
|
|
75
|
|
|
$query->execute(); |
76
|
|
|
if ($query->rowCount()) { |
77
|
|
|
$found = new static; |
78
|
|
|
$found->id = $value; |
|
|
|
|
79
|
|
|
$found->data = $query->fetchAll($connection::FETCH_ASSOC); |
|
|
|
|
80
|
|
|
return $found; |
81
|
|
|
} |
82
|
|
|
throw new DataNotFoundException(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* save() |
87
|
|
|
* Insert data into database |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function save($dbConnection = NULL) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
94
|
|
|
|
95
|
|
|
$query = $this->insertQuery(self::getTableName($connection)); |
96
|
|
|
$statement = $connection->prepare($query); |
|
|
|
|
97
|
|
|
if($statement->execute()) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
throw new DataAlreadyExistException(); |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* update() |
106
|
|
|
* Update details in database after ::find(2) |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function update($id, $dbConnection = NULL) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
113
|
|
|
|
114
|
|
|
$updateQuery = $this->updateQuery(self::getTableName($connection), $id); |
115
|
|
|
$statement = $connection->prepare($updateQuery); |
|
|
|
|
116
|
|
|
if ($statement->execute()) { |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
throw new DataAlreadyExistException(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* destroy($value) |
124
|
|
|
* Delete data from database |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function destroy($value, $dbConnection = NULL) |
129
|
|
|
{ |
130
|
|
|
$connection = DatabaseQuery::checkConnection($dbConnection); |
131
|
|
|
|
132
|
|
|
$query = $connection->prepare('DELETE FROM ' . self::getTableName($connection) . ' WHERE id = '.$value); |
|
|
|
|
133
|
|
|
$query->execute(); |
134
|
|
|
$check = $query->rowCount(); |
135
|
|
|
if ($check) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
throw new DataNotFoundException; |
139
|
|
|
} |
140
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.