@@ -12,9 +12,9 @@ |
||
| 12 | 12 | |
| 13 | 13 | class EmptyArrayException extends Exception { |
| 14 | 14 | |
| 15 | - public static function checkEmptyArrayException($message) |
|
| 16 | - { |
|
| 17 | - return new static($message); |
|
| 18 | - } |
|
| 15 | + public static function checkEmptyArrayException($message) |
|
| 16 | + { |
|
| 17 | + return new static($message); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | 20 | } |
@@ -12,9 +12,9 @@ |
||
| 12 | 12 | |
| 13 | 13 | class NoArgumentPassedToFunctionException extends Exception { |
| 14 | 14 | |
| 15 | - public static function checkNoArgumentPassedToFunction($message) |
|
| 16 | - { |
|
| 17 | - return new static($message); |
|
| 18 | - } |
|
| 15 | + public static function checkNoArgumentPassedToFunction($message) |
|
| 16 | + { |
|
| 17 | + return new static($message); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | 20 | } |
@@ -12,8 +12,8 @@ |
||
| 12 | 12 | |
| 13 | 13 | class NoRecordInsertionException extends Exception { |
| 14 | 14 | |
| 15 | - public static function checkNoRecordAddedException($mesaage) |
|
| 16 | - { |
|
| 17 | - return new static($mesaage); |
|
| 18 | - } |
|
| 15 | + public static function checkNoRecordAddedException($mesaage) |
|
| 16 | + { |
|
| 17 | + return new static($mesaage); |
|
| 18 | + } |
|
| 19 | 19 | } |
@@ -12,9 +12,9 @@ |
||
| 12 | 12 | |
| 13 | 13 | class NoRecordUpdateException extends Exception { |
| 14 | 14 | |
| 15 | - public static function checkNoRecordUpdateException($message) |
|
| 16 | - { |
|
| 17 | - return new static($message); |
|
| 18 | - } |
|
| 15 | + public static function checkNoRecordUpdateException($message) |
|
| 16 | + { |
|
| 17 | + return new static($message); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | 20 | } |
@@ -12,9 +12,9 @@ |
||
| 12 | 12 | |
| 13 | 13 | class WrongArgumentException extends Exception { |
| 14 | 14 | |
| 15 | - public function checkWrongArgumentException($message) |
|
| 16 | - { |
|
| 17 | - return new static ($message); |
|
| 18 | - } |
|
| 15 | + public function checkWrongArgumentException($message) |
|
| 16 | + { |
|
| 17 | + return new static ($message); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | 20 | } |
@@ -13,25 +13,25 @@ |
||
| 13 | 13 | |
| 14 | 14 | class DatabaseHelper { |
| 15 | 15 | |
| 16 | - public $dbConn; |
|
| 16 | + public $dbConn; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * This is a constructor; a default method that will be called automatically during class instantiation |
|
| 20 | - */ |
|
| 21 | - public function __construct($dbConnect) |
|
| 22 | - { |
|
| 23 | - $this->dbConn = $dbConnect; |
|
| 24 | - } |
|
| 18 | + /** |
|
| 19 | + * This is a constructor; a default method that will be called automatically during class instantiation |
|
| 20 | + */ |
|
| 21 | + public function __construct($dbConnect) |
|
| 22 | + { |
|
| 23 | + $this->dbConn = $dbConnect; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * This method creates a particular table |
|
| 28 | - * @param tableName |
|
| 29 | - * $return boolean true or false |
|
| 30 | - */ |
|
| 26 | + /** |
|
| 27 | + * This method creates a particular table |
|
| 28 | + * @param tableName |
|
| 29 | + * $return boolean true or false |
|
| 30 | + */ |
|
| 31 | 31 | public function createTable($tableName, $conn = NULL) |
| 32 | 32 | { |
| 33 | 33 | if (is_null($conn)) { |
| 34 | - $conn = $this->dbConn; |
|
| 34 | + $conn = $this->dbConn; |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | $sql = 'CREATE TABLE IF NOT EXISTS '.$tableName.'('; |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | /** |
| 100 | 100 | * This method find a record by id |
| 101 | 101 | * @params int id |
| 102 | - * @return Object |
|
| 102 | + * @return BaseClass |
|
| 103 | 103 | * @throws NoArgumentPassedToFunctionException |
| 104 | 104 | */ |
| 105 | 105 | public static function find($id) |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | /** |
| 143 | 143 | * This method return the current class name |
| 144 | 144 | * $params void |
| 145 | - * @return classname |
|
| 145 | + * @return string|false |
|
| 146 | 146 | */ |
| 147 | 147 | public static function getClassName() |
| 148 | 148 | { |
@@ -20,124 +20,124 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | class BaseClass implements InterfaceBaseClass |
| 22 | 22 | { |
| 23 | - protected $databaseModel; // Private variable that contains instance of database |
|
| 24 | - protected $tableName; // Class variable holding class name pluralized |
|
| 25 | - protected $properties = []; // Properties will later contain key, value pairs from the magic setter, getter methods |
|
| 26 | - use Inflector; // Inject the inflector trait |
|
| 23 | + protected $databaseModel; // Private variable that contains instance of database |
|
| 24 | + protected $tableName; // Class variable holding class name pluralized |
|
| 25 | + protected $properties = []; // Properties will later contain key, value pairs from the magic setter, getter methods |
|
| 26 | + use Inflector; // Inject the inflector trait |
|
| 27 | 27 | |
| 28 | - public function __construct() |
|
| 29 | - { |
|
| 30 | - $this->tableName = $this->getClassName(); |
|
| 31 | - $this->databaseModel = new DatabaseHandler($this->tableName); |
|
| 32 | - $this->properties['id'] = 0; |
|
| 33 | - } |
|
| 28 | + public function __construct() |
|
| 29 | + { |
|
| 30 | + $this->tableName = $this->getClassName(); |
|
| 31 | + $this->databaseModel = new DatabaseHandler($this->tableName); |
|
| 32 | + $this->properties['id'] = 0; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The magic getter method |
|
| 37 | - * @params key |
|
| 38 | - * @return array key |
|
| 39 | - */ |
|
| 40 | - public function __get($key) |
|
| 41 | - { |
|
| 42 | - $this->properties[$key]; |
|
| 43 | - } |
|
| 35 | + /** |
|
| 36 | + * The magic getter method |
|
| 37 | + * @params key |
|
| 38 | + * @return array key |
|
| 39 | + */ |
|
| 40 | + public function __get($key) |
|
| 41 | + { |
|
| 42 | + $this->properties[$key]; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * The magic setter method |
|
| 47 | - * @params property, key |
|
| 48 | - * @return array associative array properties |
|
| 49 | - */ |
|
| 50 | - public function __set($property, $value) |
|
| 51 | - { |
|
| 52 | - $this->properties[$property] = $value; |
|
| 53 | - } |
|
| 45 | + /** |
|
| 46 | + * The magic setter method |
|
| 47 | + * @params property, key |
|
| 48 | + * @return array associative array properties |
|
| 49 | + */ |
|
| 50 | + public function __set($property, $value) |
|
| 51 | + { |
|
| 52 | + $this->properties[$property] = $value; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * This method gets all the record from a particular table |
|
| 57 | - * @params void |
|
| 58 | - * @return associative array |
|
| 59 | - * @throws NoRecordFoundException |
|
| 60 | - */ |
|
| 61 | - public static function getAll() |
|
| 62 | - { |
|
| 63 | - $allData = DatabaseHandler::read($id = false, self::getClassName()); |
|
| 64 | - if (count($allData) > 0) { |
|
| 65 | - return $allData; |
|
| 66 | - } |
|
| 67 | - throw NoRecordFoundException::checkNoRecordFoundException("There is no record to display"); |
|
| 68 | - } |
|
| 55 | + /** |
|
| 56 | + * This method gets all the record from a particular table |
|
| 57 | + * @params void |
|
| 58 | + * @return associative array |
|
| 59 | + * @throws NoRecordFoundException |
|
| 60 | + */ |
|
| 61 | + public static function getAll() |
|
| 62 | + { |
|
| 63 | + $allData = DatabaseHandler::read($id = false, self::getClassName()); |
|
| 64 | + if (count($allData) > 0) { |
|
| 65 | + return $allData; |
|
| 66 | + } |
|
| 67 | + throw NoRecordFoundException::checkNoRecordFoundException("There is no record to display"); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * This method create or update record in a database table |
|
| 72 | - * @params void |
|
| 73 | - * @return bool true or false; |
|
| 74 | - * @throws EmptyArrayException |
|
| 75 | - * @throws NoRecordInsertionException |
|
| 76 | - * @throws NoRecordUpdateException |
|
| 77 | - */ |
|
| 78 | - public function save() |
|
| 79 | - { |
|
| 80 | - $boolCommit = false; |
|
| 81 | - if ($this->properties['id']) { |
|
| 82 | - $allData = DatabaseHandler::read($id = $this->properties['id'], self::getClassName()); |
|
| 83 | - if ($this->checkIfRecordIsEmpty($allData)) { |
|
| 84 | - $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties); |
|
| 85 | - if ($boolCommit) { |
|
| 86 | - return true; |
|
| 87 | - } |
|
| 88 | - throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
| 89 | - } |
|
| 90 | - throw EmptyArrayException::checkEmptyArrayException("Value passed didn't match any record"); |
|
| 91 | - } |
|
| 92 | - $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
| 93 | - if ($boolCommit) { |
|
| 94 | - return true; |
|
| 95 | - } |
|
| 96 | - throw NoRecordInsertionException::checkNoRecordAddedException("Record not created successfully"); |
|
| 97 | - } |
|
| 70 | + /** |
|
| 71 | + * This method create or update record in a database table |
|
| 72 | + * @params void |
|
| 73 | + * @return bool true or false; |
|
| 74 | + * @throws EmptyArrayException |
|
| 75 | + * @throws NoRecordInsertionException |
|
| 76 | + * @throws NoRecordUpdateException |
|
| 77 | + */ |
|
| 78 | + public function save() |
|
| 79 | + { |
|
| 80 | + $boolCommit = false; |
|
| 81 | + if ($this->properties['id']) { |
|
| 82 | + $allData = DatabaseHandler::read($id = $this->properties['id'], self::getClassName()); |
|
| 83 | + if ($this->checkIfRecordIsEmpty($allData)) { |
|
| 84 | + $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties); |
|
| 85 | + if ($boolCommit) { |
|
| 86 | + return true; |
|
| 87 | + } |
|
| 88 | + throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
| 89 | + } |
|
| 90 | + throw EmptyArrayException::checkEmptyArrayException("Value passed didn't match any record"); |
|
| 91 | + } |
|
| 92 | + $boolCommit = $this->databaseModel->create($this->properties, $this->tableName); |
|
| 93 | + if ($boolCommit) { |
|
| 94 | + return true; |
|
| 95 | + } |
|
| 96 | + throw NoRecordInsertionException::checkNoRecordAddedException("Record not created successfully"); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * This method find a record by id |
|
| 101 | - * @params int id |
|
| 102 | - * @return Object |
|
| 103 | - * @throws NoArgumentPassedToFunctionException |
|
| 104 | - */ |
|
| 105 | - public static function find($id) |
|
| 106 | - { |
|
| 107 | - $num_args = (int) func_num_args(); // get number of arguments passed to |
|
| 108 | - if ($num_args == 0 || $num_args > 1) { |
|
| 109 | - throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
| 110 | - } |
|
| 111 | - if ($id == "") { |
|
| 112 | - throw NullArgumentPassedToFunction::checkNullArgumentPassedToFunction("This function expect a value"); |
|
| 113 | - } |
|
| 114 | - $staticFindInstance = new static(); |
|
| 115 | - $staticFindInstance->id = $id == "" ? false : $id; |
|
| 116 | - return $staticFindInstance; |
|
| 99 | + /** |
|
| 100 | + * This method find a record by id |
|
| 101 | + * @params int id |
|
| 102 | + * @return Object |
|
| 103 | + * @throws NoArgumentPassedToFunctionException |
|
| 104 | + */ |
|
| 105 | + public static function find($id) |
|
| 106 | + { |
|
| 107 | + $num_args = (int) func_num_args(); // get number of arguments passed to |
|
| 108 | + if ($num_args == 0 || $num_args > 1) { |
|
| 109 | + throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
| 110 | + } |
|
| 111 | + if ($id == "") { |
|
| 112 | + throw NullArgumentPassedToFunction::checkNullArgumentPassedToFunction("This function expect a value"); |
|
| 113 | + } |
|
| 114 | + $staticFindInstance = new static(); |
|
| 115 | + $staticFindInstance->id = $id == "" ? false : $id; |
|
| 116 | + return $staticFindInstance; |
|
| 117 | 117 | |
| 118 | - } |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * This method delete a row from the table by the row id |
|
| 122 | - * @params int id |
|
| 123 | - * @return boolean true or false |
|
| 124 | - * @throws NoRecordDeletionException; |
|
| 125 | - */ |
|
| 126 | - public static function destroy($id) |
|
| 127 | - { |
|
| 128 | - $boolDeleted = false; |
|
| 129 | - $num_args = (int) func_num_args(); // get number of arguments passed to |
|
| 120 | + /** |
|
| 121 | + * This method delete a row from the table by the row id |
|
| 122 | + * @params int id |
|
| 123 | + * @return boolean true or false |
|
| 124 | + * @throws NoRecordDeletionException; |
|
| 125 | + */ |
|
| 126 | + public static function destroy($id) |
|
| 127 | + { |
|
| 128 | + $boolDeleted = false; |
|
| 129 | + $num_args = (int) func_num_args(); // get number of arguments passed to |
|
| 130 | 130 | |
| 131 | - if ($num_args == 0 || $num_args > 1) { |
|
| 132 | - throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
| 133 | - } |
|
| 134 | - $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
| 135 | - if ($boolDeleted) { |
|
| 136 | - return true; |
|
| 137 | - } |
|
| 138 | - throw NoRecordDeletionException::checkNoRecordUpdateException("Record deletion unsuccessful because id does not match any record"); |
|
| 131 | + if ($num_args == 0 || $num_args > 1) { |
|
| 132 | + throw NoArgumentPassedToFunctionException::checkNoArgumentPassedToFunction("Argument missing: only one argument is allowed"); |
|
| 133 | + } |
|
| 134 | + $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
|
| 135 | + if ($boolDeleted) { |
|
| 136 | + return true; |
|
| 137 | + } |
|
| 138 | + throw NoRecordDeletionException::checkNoRecordUpdateException("Record deletion unsuccessful because id does not match any record"); |
|
| 139 | 139 | |
| 140 | - } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * This method return the current class name |
@@ -156,12 +156,12 @@ discard block |
||
| 156 | 156 | * @param $arrayOfRecord |
| 157 | 157 | * @return bool |
| 158 | 158 | */ |
| 159 | - public function checkIfRecordIsEmpty($arrayOfRecord) |
|
| 160 | - { |
|
| 161 | - if (count($arrayOfRecord) > 0) { |
|
| 162 | - return true; |
|
| 163 | - } |
|
| 164 | - return false; |
|
| 165 | - } |
|
| 159 | + public function checkIfRecordIsEmpty($arrayOfRecord) |
|
| 160 | + { |
|
| 161 | + if (count($arrayOfRecord) > 0) { |
|
| 162 | + return true; |
|
| 163 | + } |
|
| 164 | + return false; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | 167 | } |
@@ -10,12 +10,12 @@ |
||
| 10 | 10 | |
| 11 | 11 | interface InterfaceBaseClass { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * This method gets all the record from a particular table |
|
| 15 | - * @params void |
|
| 16 | - * @return associative array |
|
| 17 | - */ |
|
| 18 | - public static function getAll(); |
|
| 13 | + /** |
|
| 14 | + * This method gets all the record from a particular table |
|
| 15 | + * @params void |
|
| 16 | + * @return associative array |
|
| 17 | + */ |
|
| 18 | + public static function getAll(); |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * This method create or update record in a database table |
@@ -21,6 +21,7 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * This is a constructor; a default method that will be called automatically during class instantiation |
| 24 | + * @param string|false $modelClassName |
|
| 24 | 25 | */ |
| 25 | 26 | public function __construct($modelClassName, $dbConn = Null) |
| 26 | 27 | { |
@@ -35,6 +36,7 @@ discard block |
||
| 35 | 36 | /** |
| 36 | 37 | * This method create a record and store it in a table row |
| 37 | 38 | * @params associative array, string tablename |
| 39 | + * @param string|false $tableName |
|
| 38 | 40 | * @return boolean true or false |
| 39 | 41 | */ |
| 40 | 42 | public function create($associative1DArray, $tableName, $dbConn = Null) |
@@ -69,6 +71,7 @@ discard block |
||
| 69 | 71 | /** |
| 70 | 72 | * This method updates any table by supplying 3 parameter |
| 71 | 73 | * @params: $updateParams, $tableName, $associative1DArray |
| 74 | + * @param string|false $tableName |
|
| 72 | 75 | * @return boolean true or false |
| 73 | 76 | */ |
| 74 | 77 | public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = Null) |
@@ -101,6 +104,7 @@ discard block |
||
| 101 | 104 | /** |
| 102 | 105 | * This method retrieves record from a table |
| 103 | 106 | * @params int id, string tableName |
| 107 | + * @param string|false $tableName |
|
| 104 | 108 | * @return array |
| 105 | 109 | */ |
| 106 | 110 | public static function read($id, $tableName, $dbConn = Null) |
@@ -131,6 +135,7 @@ discard block |
||
| 131 | 135 | /** |
| 132 | 136 | * This method deletes a record from a table row |
| 133 | 137 | * @params int id, string tableName |
| 138 | + * @param string|false $tableName |
|
| 134 | 139 | * @return boolean true or false |
| 135 | 140 | */ |
| 136 | 141 | public static function delete($id, $tableName, $dbConn = Null) |
@@ -163,7 +168,7 @@ discard block |
||
| 163 | 168 | |
| 164 | 169 | /** |
| 165 | 170 | * This method returns sql query |
| 166 | - * @param $sql |
|
| 171 | + * @param string $sql |
|
| 167 | 172 | * @return string |
| 168 | 173 | */ |
| 169 | 174 | public function prepareUpdateQuery($sql) |
@@ -204,6 +209,7 @@ discard block |
||
| 204 | 209 | * This method returns column fields of a particular table |
| 205 | 210 | * @param $table |
| 206 | 211 | * @param $conn |
| 212 | + * @param DatabaseConnection $dbConn |
|
| 207 | 213 | * @return array |
| 208 | 214 | */ |
| 209 | 215 | public function getColumnNames($table, $dbConn = Null) { |
@@ -18,105 +18,105 @@ discard block |
||
| 18 | 18 | use Laztopaz\potatoORM\NoRecordUpdateException; |
| 19 | 19 | |
| 20 | 20 | class DatabaseHandler { |
| 21 | - private $tableFields; |
|
| 22 | - private $dbHelperInstance; |
|
| 23 | - private $dbConnection; |
|
| 24 | - private $model; |
|
| 21 | + private $tableFields; |
|
| 22 | + private $dbHelperInstance; |
|
| 23 | + private $dbConnection; |
|
| 24 | + private $model; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * This is a constructor; a default method that will be called automatically during class instantiation |
|
| 28 | - */ |
|
| 29 | - public function __construct($modelClassName, $dbConn = Null) { |
|
| 30 | - if (is_null($dbConn)) { |
|
| 31 | - $this->dbConnection = new DatabaseConnection(); |
|
| 32 | - } else { |
|
| 33 | - $this->dbConnection = $dbConn; |
|
| 34 | - } |
|
| 35 | - $this->model = $modelClassName; |
|
| 36 | - } |
|
| 26 | + /** |
|
| 27 | + * This is a constructor; a default method that will be called automatically during class instantiation |
|
| 28 | + */ |
|
| 29 | + public function __construct($modelClassName, $dbConn = Null) { |
|
| 30 | + if (is_null($dbConn)) { |
|
| 31 | + $this->dbConnection = new DatabaseConnection(); |
|
| 32 | + } else { |
|
| 33 | + $this->dbConnection = $dbConn; |
|
| 34 | + } |
|
| 35 | + $this->model = $modelClassName; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * This method create a record and store it in a table row |
|
| 40 | - * @params associative array, string tablename |
|
| 41 | - * @return boolean true or false |
|
| 42 | - */ |
|
| 43 | - public function create($associative1DArray, $tableName, $dbConn = Null) { |
|
| 44 | - $tableFields = $this->getColumnNames($this->model, $this->dbConnection); |
|
| 45 | - $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($tableFields,$associative1DArray); |
|
| 46 | - if (count($unexpectedFields) > 0) { |
|
| 47 | - throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields,"needs to be created as table field"); |
|
| 48 | - } |
|
| 49 | - unset($associative1DArray[0]); |
|
| 50 | - if (is_null($dbConn)) { |
|
| 51 | - $dbConn = $this->dbConnection; |
|
| 52 | - } |
|
| 53 | - return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
| 54 | - } |
|
| 38 | + /** |
|
| 39 | + * This method create a record and store it in a table row |
|
| 40 | + * @params associative array, string tablename |
|
| 41 | + * @return boolean true or false |
|
| 42 | + */ |
|
| 43 | + public function create($associative1DArray, $tableName, $dbConn = Null) { |
|
| 44 | + $tableFields = $this->getColumnNames($this->model, $this->dbConnection); |
|
| 45 | + $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($tableFields,$associative1DArray); |
|
| 46 | + if (count($unexpectedFields) > 0) { |
|
| 47 | + throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields,"needs to be created as table field"); |
|
| 48 | + } |
|
| 49 | + unset($associative1DArray[0]); |
|
| 50 | + if (is_null($dbConn)) { |
|
| 51 | + $dbConn = $this->dbConnection; |
|
| 52 | + } |
|
| 53 | + return $this->insertRecord($dbConn, $tableName, $associative1DArray); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * This method runs the insertion query |
|
| 58 | - * @param $dbConn |
|
| 59 | - * @param $tableName |
|
| 60 | - * @param $associative1DArray |
|
| 61 | - * @return boolean true |
|
| 62 | - */ |
|
| 63 | - private function insertRecord($dbConn, $tableName, $associative1DArray) { |
|
| 64 | - $insertQuery = 'INSERT INTO '.$tableName; |
|
| 65 | - $TableValues = implode(',',array_keys($associative1DArray)); |
|
| 66 | - foreach ($associative1DArray as $field => $value) { |
|
| 67 | - $FormValues[] = "'".trim(addslashes($value))."'"; |
|
| 68 | - } |
|
| 69 | - $splittedTableValues = implode(',', $FormValues); |
|
| 70 | - $insertQuery.= ' ('.$TableValues.')'; |
|
| 71 | - $insertQuery.= ' VALUES ('.$splittedTableValues.')'; |
|
| 72 | - $executeQuery = $dbConn->exec($insertQuery); |
|
| 73 | - |
|
| 74 | - if ($executeQuery) { |
|
| 75 | - return true; |
|
| 76 | - } |
|
| 77 | - return false; |
|
| 78 | - |
|
| 79 | - throw NoRecordInsertionException::checkNoRecordAddedException("Record not inserted successfully"); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * This method updates any table by supplying 3 parameter |
|
| 84 | - * @params: $updateParams, $tableName, $associative1DArray |
|
| 85 | - * @return boolean true or false |
|
| 86 | - */ |
|
| 87 | - public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = Null) |
|
| 88 | - { |
|
| 89 | - $sql = ""; |
|
| 90 | - if (is_null($dbConn)) { |
|
| 91 | - $dbConn = $this->dbConnection; |
|
| 92 | - } |
|
| 93 | - $updateSql = "UPDATE `$tableName` SET "; |
|
| 94 | - unset($associative1DArray['id']); |
|
| 95 | - $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection),$associative1DArray); |
|
| 56 | + /** |
|
| 57 | + * This method runs the insertion query |
|
| 58 | + * @param $dbConn |
|
| 59 | + * @param $tableName |
|
| 60 | + * @param $associative1DArray |
|
| 61 | + * @return boolean true |
|
| 62 | + */ |
|
| 63 | + private function insertRecord($dbConn, $tableName, $associative1DArray) { |
|
| 64 | + $insertQuery = 'INSERT INTO '.$tableName; |
|
| 65 | + $TableValues = implode(',',array_keys($associative1DArray)); |
|
| 66 | + foreach ($associative1DArray as $field => $value) { |
|
| 67 | + $FormValues[] = "'".trim(addslashes($value))."'"; |
|
| 68 | + } |
|
| 69 | + $splittedTableValues = implode(',', $FormValues); |
|
| 70 | + $insertQuery.= ' ('.$TableValues.')'; |
|
| 71 | + $insertQuery.= ' VALUES ('.$splittedTableValues.')'; |
|
| 72 | + $executeQuery = $dbConn->exec($insertQuery); |
|
| 73 | + |
|
| 74 | + if ($executeQuery) { |
|
| 75 | + return true; |
|
| 76 | + } |
|
| 77 | + return false; |
|
| 78 | + |
|
| 79 | + throw NoRecordInsertionException::checkNoRecordAddedException("Record not inserted successfully"); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * This method updates any table by supplying 3 parameter |
|
| 84 | + * @params: $updateParams, $tableName, $associative1DArray |
|
| 85 | + * @return boolean true or false |
|
| 86 | + */ |
|
| 87 | + public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = Null) |
|
| 88 | + { |
|
| 89 | + $sql = ""; |
|
| 90 | + if (is_null($dbConn)) { |
|
| 91 | + $dbConn = $this->dbConnection; |
|
| 92 | + } |
|
| 93 | + $updateSql = "UPDATE `$tableName` SET "; |
|
| 94 | + unset($associative1DArray['id']); |
|
| 95 | + $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection),$associative1DArray); |
|
| 96 | 96 | |
| 97 | - if (count($unexpectedFields) > 0) { |
|
| 98 | - throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields, "needs to be created as table field"); |
|
| 99 | - } |
|
| 100 | - foreach($associative1DArray as $field => $value) { |
|
| 101 | - $sql .= "`$field` = '$value'".","; |
|
| 102 | - } |
|
| 97 | + if (count($unexpectedFields) > 0) { |
|
| 98 | + throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields, "needs to be created as table field"); |
|
| 99 | + } |
|
| 100 | + foreach($associative1DArray as $field => $value) { |
|
| 101 | + $sql .= "`$field` = '$value'".","; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - $updateSql .= $this->prepareUpdateQuery($sql); |
|
| 104 | + $updateSql .= $this->prepareUpdateQuery($sql); |
|
| 105 | 105 | |
| 106 | - foreach ($updateParams as $key => $val) { |
|
| 107 | - $updateSql .= " WHERE $key = $val"; |
|
| 108 | - } |
|
| 109 | - $stmt = $dbConn->prepare($updateSql); |
|
| 106 | + foreach ($updateParams as $key => $val) { |
|
| 107 | + $updateSql .= " WHERE $key = $val"; |
|
| 108 | + } |
|
| 109 | + $stmt = $dbConn->prepare($updateSql); |
|
| 110 | 110 | |
| 111 | - $boolResponse = $stmt->execute(); |
|
| 111 | + $boolResponse = $stmt->execute(); |
|
| 112 | 112 | |
| 113 | - if ($boolResponse) { |
|
| 114 | - return true; |
|
| 115 | - } |
|
| 116 | - return false; |
|
| 113 | + if ($boolResponse) { |
|
| 114 | + return true; |
|
| 115 | + } |
|
| 116 | + return false; |
|
| 117 | 117 | |
| 118 | - throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
| 119 | - } |
|
| 118 | + throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully"); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | /** |
| 122 | 122 | * This method retrieves record from a table |
@@ -128,22 +128,22 @@ discard block |
||
| 128 | 128 | $tableData = []; |
| 129 | 129 | |
| 130 | 130 | if (is_null($dbConn)) { |
| 131 | - $dbConn = new DatabaseConnection(); |
|
| 131 | + $dbConn = new DatabaseConnection(); |
|
| 132 | 132 | } |
| 133 | 133 | $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName; |
| 134 | 134 | |
| 135 | 135 | try { |
| 136 | - $stmt = $dbConn->prepare($sql); |
|
| 137 | - $stmt->bindValue(':table', $tableName); |
|
| 138 | - $stmt->bindValue(':id', $id); |
|
| 139 | - $stmt->execute(); |
|
| 136 | + $stmt = $dbConn->prepare($sql); |
|
| 137 | + $stmt->bindValue(':table', $tableName); |
|
| 138 | + $stmt->bindValue(':id', $id); |
|
| 139 | + $stmt->execute(); |
|
| 140 | 140 | } catch (PDOException $e) { |
| 141 | - return $e->getMessage(); |
|
| 141 | + return $e->getMessage(); |
|
| 142 | 142 | } |
| 143 | 143 | $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 144 | 144 | |
| 145 | 145 | foreach($results as $result) { |
| 146 | - array_push($tableData, $result); |
|
| 146 | + array_push($tableData, $result); |
|
| 147 | 147 | } |
| 148 | 148 | return $tableData; |
| 149 | 149 | } |
@@ -155,20 +155,20 @@ discard block |
||
| 155 | 155 | */ |
| 156 | 156 | public static function delete($id, $tableName, $dbConn = Null) |
| 157 | 157 | { |
| 158 | - if (is_null($dbConn)) { |
|
| 159 | - $dbConn = new DatabaseConnection(); |
|
| 160 | - } |
|
| 158 | + if (is_null($dbConn)) { |
|
| 159 | + $dbConn = new DatabaseConnection(); |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
| 162 | + $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id; |
|
| 163 | 163 | |
| 164 | - $boolResponse = $dbConn->exec($sql); |
|
| 164 | + $boolResponse = $dbConn->exec($sql); |
|
| 165 | 165 | |
| 166 | - if ($boolResponse) { |
|
| 166 | + if ($boolResponse) { |
|
| 167 | 167 | |
| 168 | - return true; |
|
| 169 | - } |
|
| 168 | + return true; |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - throw NoRecordDeletionException::checkNoRecordDeleteException("Record not deleted successfully"); |
|
| 171 | + throw NoRecordDeletionException::checkNoRecordDeleteException("Record not deleted successfully"); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -179,13 +179,13 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | public static function checkIfMagicSetterContainsIsSameAsClassModel(array $tableColumn, array $userSetterArray) |
| 181 | 181 | { |
| 182 | - $unexpectedFields = []; |
|
| 183 | - foreach ($userSetterArray as $key => $val) { |
|
| 184 | - if (!in_array($key,$tableColumn)) { |
|
| 185 | - $unexpectedFields[] = $key; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - return $unexpectedFields; |
|
| 182 | + $unexpectedFields = []; |
|
| 183 | + foreach ($userSetterArray as $key => $val) { |
|
| 184 | + if (!in_array($key,$tableColumn)) { |
|
| 185 | + $unexpectedFields[] = $key; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + return $unexpectedFields; |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /** |
@@ -195,10 +195,10 @@ discard block |
||
| 195 | 195 | */ |
| 196 | 196 | public function prepareUpdateQuery($sql) |
| 197 | 197 | { |
| 198 | - $splittedQuery = explode(",",$sql); |
|
| 199 | - array_pop($splittedQuery); |
|
| 200 | - $mergeData = implode(",",$splittedQuery); |
|
| 201 | - return $mergeData; |
|
| 198 | + $splittedQuery = explode(",",$sql); |
|
| 199 | + array_pop($splittedQuery); |
|
| 200 | + $mergeData = implode(",",$splittedQuery); |
|
| 201 | + return $mergeData; |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | /** |
@@ -210,21 +210,21 @@ discard block |
||
| 210 | 210 | */ |
| 211 | 211 | public function findAndWhere($params, $tableName, $dbConn = Null) |
| 212 | 212 | { |
| 213 | - if (is_null($dbConn)) { |
|
| 214 | - $dbConn = $this->dbConnection; |
|
| 215 | - } |
|
| 216 | - if (is_array($params) && !empty($params)) { |
|
| 217 | - $sql = "SELECT * FROM ".$tableName; |
|
| 218 | - foreach ($params as $key => $val) { |
|
| 219 | - $sql .= " WHERE `$key` = '$val'"; |
|
| 220 | - } |
|
| 221 | - $statement = $dbConn->prepare($sql); |
|
| 222 | - $statement->execute(); |
|
| 223 | - $returnedRowNumbers = $statement->rowCount(); |
|
| 213 | + if (is_null($dbConn)) { |
|
| 214 | + $dbConn = $this->dbConnection; |
|
| 215 | + } |
|
| 216 | + if (is_array($params) && !empty($params)) { |
|
| 217 | + $sql = "SELECT * FROM ".$tableName; |
|
| 218 | + foreach ($params as $key => $val) { |
|
| 219 | + $sql .= " WHERE `$key` = '$val'"; |
|
| 220 | + } |
|
| 221 | + $statement = $dbConn->prepare($sql); |
|
| 222 | + $statement->execute(); |
|
| 223 | + $returnedRowNumbers = $statement->rowCount(); |
|
| 224 | 224 | |
| 225 | - return $returnedRowNumbers ? true : false; |
|
| 226 | - } |
|
| 227 | - throw EmptyArrayException::checkEmptyArrayException("Array Expected: parameter passed to this function is not an array"); |
|
| 225 | + return $returnedRowNumbers ? true : false; |
|
| 226 | + } |
|
| 227 | + throw EmptyArrayException::checkEmptyArrayException("Array Expected: parameter passed to this function is not an array"); |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | /** |
@@ -234,22 +234,22 @@ discard block |
||
| 234 | 234 | * @return array |
| 235 | 235 | */ |
| 236 | 236 | public function getColumnNames($table, $dbConn = Null) { |
| 237 | - $tableFields = []; |
|
| 237 | + $tableFields = []; |
|
| 238 | 238 | |
| 239 | - if (is_null($dbConn)) { |
|
| 240 | - $dbConn = $this->dbConnection; |
|
| 241 | - } |
|
| 242 | - $sql = "SHOW COLUMNS FROM ".$table; |
|
| 239 | + if (is_null($dbConn)) { |
|
| 240 | + $dbConn = $this->dbConnection; |
|
| 241 | + } |
|
| 242 | + $sql = "SHOW COLUMNS FROM ".$table; |
|
| 243 | 243 | |
| 244 | - $stmt = $dbConn->prepare($sql); |
|
| 245 | - $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
| 246 | - $stmt->execute(); |
|
| 247 | - $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
| 244 | + $stmt = $dbConn->prepare($sql); |
|
| 245 | + $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
|
| 246 | + $stmt->execute(); |
|
| 247 | + $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
|
| 248 | 248 | |
| 249 | - foreach($results as $result) { |
|
| 250 | - array_push($tableFields, $result['Field']); |
|
| 251 | - } |
|
| 252 | - return $tableFields; |
|
| 249 | + foreach($results as $result) { |
|
| 250 | + array_push($tableFields, $result['Field']); |
|
| 251 | + } |
|
| 252 | + return $tableFields; |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | } |