1 | <?php |
||
16 | class DatabaseHandler { |
||
17 | |||
18 | private $tableFields; |
||
|
|||
19 | private $dbHelperInstance; |
||
20 | private $dbConnection; |
||
21 | private $model; |
||
22 | |||
23 | /** |
||
24 | * This is a constructor; a default method that will be called automatically during class instantiation |
||
25 | */ |
||
26 | public function __construct($modelClassName, $dbConn = Null) |
||
35 | |||
36 | /** |
||
37 | * This method create a record and store it in a table row |
||
38 | * @params associative array, string tablename |
||
39 | * @return boolean true or false |
||
40 | */ |
||
41 | public function create($associative1DArray, $tableName, $dbConn = Null) |
||
42 | { |
||
43 | $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection),$associative1DArray); |
||
44 | |||
45 | if (count($unexpectedFields) > 0) { |
||
46 | throw TableFieldUndefinedException::fieldsNotDefinedException($unexpectedFields,"needs to be created as table field"); |
||
47 | } |
||
48 | // if ($this->findAndWhere(['alias' => $associative1DArray['alias']], $this->model, $this->dbConnection)) { |
||
49 | // throw NoRecordInsertionException::checkNoRecordAddedException("Insertion Error: Record already exist"); |
||
50 | // } |
||
51 | unset($this->getColumnNames($this->model, $this->dbConnection)[0]); |
||
52 | |||
53 | if (is_null($dbConn)) { |
||
54 | $dbConn = $this->dbConnection; |
||
55 | } |
||
56 | |||
57 | $this->insertRecord($dbConn, $tableName, $associative1DArray); |
||
58 | } |
||
59 | |||
60 | |||
61 | private function insertRecord($dbConn, $tableName, $associative1DArray) { |
||
76 | |||
77 | /* |
||
78 | * This method updates any table by supplying 3 parameter |
||
79 | * @params: $updateParams, $tableName, $associative1DArray |
||
80 | * @return boolean true or false |
||
81 | */ |
||
82 | public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = Null) |
||
111 | |||
112 | /** |
||
113 | * This method retrieves record from a table |
||
114 | * @params int id, string tableName |
||
115 | * @return array |
||
116 | */ |
||
117 | public static function read($id, $tableName, $dbConn = Null) |
||
142 | |||
143 | /** |
||
144 | * This method deletes a record from a table row |
||
145 | * @params int id, string tableName |
||
146 | * @return boolean true or false |
||
147 | */ |
||
148 | public static function delete($id, $tableName, $dbConn = Null) |
||
158 | |||
159 | /** |
||
160 | * This method checks if the magic setters array is the same as the table columns |
||
161 | * @param array $tableColumn |
||
162 | * @param array $userSetterArray |
||
163 | * @return array $unexpectedFields |
||
164 | */ |
||
165 | public static function checkIfMagicSetterContainsIsSameAsClassModel(array $tableColumn, array $userSetterArray) |
||
177 | |||
178 | /** |
||
179 | * This method returns sql query |
||
180 | * @param $sql |
||
181 | * @return string |
||
182 | */ |
||
183 | public function prepareUpdateQuery($sql) |
||
191 | |||
192 | /** |
||
193 | * @param array $params |
||
194 | * @param $tableName |
||
195 | * @param $dbConn |
||
196 | * @return bool |
||
197 | * @throws EmptyArrayException |
||
198 | */ |
||
199 | public function findAndWhere($params, $tableName, $dbConn) |
||
217 | |||
218 | /** |
||
219 | * This method returns column fields of a particular table |
||
220 | * @param $table |
||
221 | * @param $conn |
||
222 | * @return array |
||
223 | */ |
||
224 | public function getColumnNames($table, $dbConn = Null) { |
||
243 | |||
244 | |||
245 | } |
||
246 |
This check marks private properties in classes that are never used. Those properties can be removed.