@@ -10,28 +10,28 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * Store instance of database connection used. |
| 13 | - * @var [type] |
|
| 14 | - */ |
|
| 13 | + * @var [type] |
|
| 14 | + */ |
|
| 15 | 15 | protected $databaseConnection; |
| 16 | 16 | |
| 17 | - public function __construct() |
|
| 17 | + public function __construct() |
|
| 18 | 18 | { |
| 19 | 19 | $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
| 20 | 20 | //$databaseConnection->databaseConnection->connect(); |
| 21 | 21 | } |
| 22 | 22 | /** |
| 23 | - * @param string $key rep column name |
|
| 24 | - * @param string $val rep column value |
|
| 25 | - * sets into $propertie the $key => $value pairs |
|
| 26 | - */ |
|
| 23 | + * @param string $key rep column name |
|
| 24 | + * @param string $val rep column value |
|
| 25 | + * sets into $propertie the $key => $value pairs |
|
| 26 | + */ |
|
| 27 | 27 | public function __set($key, $val) |
| 28 | 28 | { |
| 29 | 29 | $this->properties[$key] = $val; |
| 30 | 30 | } |
| 31 | 31 | /** |
| 32 | - * @param string $key reps the column name |
|
| 33 | - * @return $key and $value |
|
| 34 | - */ |
|
| 32 | + * @param string $key reps the column name |
|
| 33 | + * @return $key and $value |
|
| 34 | + */ |
|
| 35 | 35 | public function __get($key) |
| 36 | 36 | { |
| 37 | 37 | return $this->properties[$key]; |
@@ -41,17 +41,17 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @return array |
| 43 | 43 | */ |
| 44 | - public function getProperties() |
|
| 45 | - { |
|
| 46 | - return $this->properties; |
|
| 47 | - } |
|
| 44 | + public function getProperties() |
|
| 45 | + { |
|
| 46 | + return $this->properties; |
|
| 47 | + } |
|
| 48 | 48 | /** |
| 49 | - * Gets the name of the child class only |
|
| 50 | - * without the namespace |
|
| 51 | - * @var $className |
|
| 52 | - * @var $table |
|
| 53 | - * @return $table |
|
| 54 | - */ |
|
| 49 | + * Gets the name of the child class only |
|
| 50 | + * without the namespace |
|
| 51 | + * @var $className |
|
| 52 | + * @var $table |
|
| 53 | + * @return $table |
|
| 54 | + */ |
|
| 55 | 55 | public function getTableName() |
| 56 | 56 | { |
| 57 | 57 | $className = explode('\\', get_called_class()); |
@@ -59,11 +59,11 @@ discard block |
||
| 59 | 59 | return $table; |
| 60 | 60 | } |
| 61 | 61 | /** |
| 62 | - * returns a particular record |
|
| 63 | - * @param $id reps the record id |
|
| 64 | - * @param $connection initialised to null |
|
| 65 | - * @return object |
|
| 66 | - */ |
|
| 62 | + * returns a particular record |
|
| 63 | + * @param $id reps the record id |
|
| 64 | + * @param $connection initialised to null |
|
| 65 | + * @return object |
|
| 66 | + */ |
|
| 67 | 67 | public static function find($id) |
| 68 | 68 | { |
| 69 | 69 | $model = new static; |
@@ -71,11 +71,11 @@ discard block |
||
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | - * returns a particular record |
|
| 75 | - * @param $id reps the record id |
|
| 76 | - * @param $connection initialised to null |
|
| 77 | - * @return object |
|
| 78 | - */ |
|
| 74 | + * returns a particular record |
|
| 75 | + * @param $id reps the record id |
|
| 76 | + * @param $connection initialised to null |
|
| 77 | + * @return object |
|
| 78 | + */ |
|
| 79 | 79 | public function get($id) |
| 80 | 80 | { |
| 81 | 81 | $sql = "SELECT * FROM {$this->getTableName()} WHERE id={$id}"; |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | * Update the model in the database. |
| 108 | 108 | * |
| 109 | 109 | * @return int |
| 110 | - */ |
|
| 110 | + */ |
|
| 111 | 111 | private function update() |
| 112 | 112 | { |
| 113 | 113 | |
@@ -130,10 +130,10 @@ discard block |
||
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
| 133 | - * Insert the model values into the database. |
|
| 134 | - * |
|
| 135 | - * @return int |
|
| 136 | - */ |
|
| 133 | + * Insert the model values into the database. |
|
| 134 | + * |
|
| 135 | + * @return int |
|
| 136 | + */ |
|
| 137 | 137 | private function create() |
| 138 | 138 | { |
| 139 | 139 | |
@@ -168,11 +168,11 @@ discard block |
||
| 168 | 168 | return $this->id ? $this->update() : $this->create(); |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * Delete a model from the database. |
|
| 173 | - * @param int $id |
|
| 174 | - * @return boolean |
|
| 175 | - */ |
|
| 171 | + /** |
|
| 172 | + * Delete a model from the database. |
|
| 173 | + * @param int $id |
|
| 174 | + * @return boolean |
|
| 175 | + */ |
|
| 176 | 176 | public static function destroy($id) |
| 177 | 177 | { |
| 178 | 178 | $model = new static; |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | public function getTableName() |
| 56 | 56 | { |
| 57 | 57 | $className = explode('\\', get_called_class()); |
| 58 | - $table = strtolower(end($className) .'s'); |
|
| 58 | + $table = strtolower(end($className) . 's'); |
|
| 59 | 59 | return $table; |
| 60 | 60 | } |
| 61 | 61 | /** |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | $sqlStatement = $this->databaseConnection->prepare($sql); |
| 83 | 83 | $sqlStatement->setFetchMode($this->databaseConnection::FETCH_CLASS, get_called_class()); |
| 84 | 84 | $sqlStatement->execute(); |
| 85 | - if($sqlStatement->rowCount() < 1){ |
|
| 85 | + if ($sqlStatement->rowCount() < 1) { |
|
| 86 | 86 | throw new ModelNotFoundException($id); |
| 87 | 87 | } |
| 88 | 88 | return $sqlStatement->fetch(); |
@@ -114,15 +114,15 @@ discard block |
||
| 114 | 114 | $columnNames = ""; |
| 115 | 115 | $columnValues = ""; |
| 116 | 116 | $bindNameParameters = []; |
| 117 | - $sqlUpdate = "UPDATE " . $this->getTableName() . " SET " ; |
|
| 117 | + $sqlUpdate = "UPDATE " . $this->getTableName() . " SET "; |
|
| 118 | 118 | foreach ($this->properties as $columnName => $columnValue) { |
| 119 | - if($key == 'id') continue; |
|
| 119 | + if ($key == 'id') continue; |
|
| 120 | 120 | $bindColumnName = ':' . $columnName; |
| 121 | 121 | $sqlUpdate .= "$columnName = $bindColumnName,"; |
| 122 | 122 | $bindNameParameters[$bindColumnName] = $columnValue |
| 123 | 123 | } |
| 124 | 124 | //Remove the last comma in sql command then join it to the other query part. |
| 125 | - $sqlUpdate = substr($sqlUpdate, 0, -1)." WHERE id = :id"; |
|
| 125 | + $sqlUpdate = substr($sqlUpdate, 0, -1) . " WHERE id = :id"; |
|
| 126 | 126 | $sqlStatement = $this->databaseConnection->prepare($sqlUpdate); |
| 127 | 127 | $sqlStatement->bindValue(":id", $this->properties['id']); |
| 128 | 128 | $sqlStatement->execute($bindNameParameters); |
@@ -140,19 +140,19 @@ discard block |
||
| 140 | 140 | $columnNames = ""; |
| 141 | 141 | $columnValues = ""; |
| 142 | 142 | $bindNameParameters = []; |
| 143 | - $sqlCreate = "INSERT" . " INTO " . $this->getTableName()." ("; |
|
| 143 | + $sqlCreate = "INSERT" . " INTO " . $this->getTableName() . " ("; |
|
| 144 | 144 | foreach ($this->properties as $columnName => $columnValue) { |
| 145 | 145 | |
| 146 | 146 | $bindColumnName = ':' . $columnName; |
| 147 | - $columnNames .= $columnName.","; |
|
| 148 | - $columnValues .= $bindColumnName.","; |
|
| 147 | + $columnNames .= $columnName . ","; |
|
| 148 | + $columnValues .= $bindColumnName . ","; |
|
| 149 | 149 | $bindNameParameters[$bindColumnName] = $columnValue |
| 150 | 150 | } |
| 151 | 151 | // Remove ending comma and whitespace. |
| 152 | 152 | $columnNames = substr($columnNames, 0, -1); |
| 153 | 153 | $columnValues = substr($columnValues, 0, -1); |
| 154 | 154 | |
| 155 | - $sqlCreate .= $columnNames.') VALUES (' .$columnValues.')'; |
|
| 155 | + $sqlCreate .= $columnNames . ') VALUES (' . $columnValues . ')'; |
|
| 156 | 156 | $sqlStatement = $this->databaseConnection->prepare($sqlCreate); |
| 157 | 157 | $sqlStatement->execute($bindNameParameters); |
| 158 | 158 | return $sqlStatement->rowCount(); |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | */ |
| 188 | 188 | public function delete($id) |
| 189 | 189 | { |
| 190 | - $sql = "DELETE" . " FROM " . self::getTableName()." WHERE id = ". $id; |
|
| 190 | + $sql = "DELETE" . " FROM " . self::getTableName() . " WHERE id = " . $id; |
|
| 191 | 191 | $sqlStatment = $this->databaseConnection->prepare($sql); |
| 192 | 192 | $sqlStatment->execute(); |
| 193 | 193 | return ($sqlStatment->rowCount() > 0) ? true : false; |
@@ -116,7 +116,9 @@ |
||
| 116 | 116 | $bindNameParameters = []; |
| 117 | 117 | $sqlUpdate = "UPDATE " . $this->getTableName() . " SET " ; |
| 118 | 118 | foreach ($this->properties as $columnName => $columnValue) { |
| 119 | - if($key == 'id') continue; |
|
| 119 | + if($key == 'id') { |
|
| 120 | + continue; |
|
| 121 | + } |
|
| 120 | 122 | $bindColumnName = ':' . $columnName; |
| 121 | 123 | $sqlUpdate .= "$columnName = $bindColumnName,"; |
| 122 | 124 | $bindNameParameters[$bindColumnName] = $columnValue |