@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | /** |
| 130 | 130 | * Mark the field as dirty, so it will be set in inserts and updates |
| 131 | 131 | * |
| 132 | - * @param $name |
|
| 132 | + * @param string $name |
|
| 133 | 133 | */ |
| 134 | 134 | public function markFieldDirty( $name ) { |
| 135 | 135 | $this->dirty->$name = true; // field became dirty |
@@ -443,7 +443,7 @@ discard block |
||
| 443 | 443 | * find one match based on a single field and match criteria |
| 444 | 444 | * |
| 445 | 445 | * @param string $fieldname |
| 446 | - * @param mixed $match string|array |
|
| 446 | + * @param string $match string|array |
|
| 447 | 447 | * @param string $order ASC|DESC |
| 448 | 448 | * @return object of calling class |
| 449 | 449 | */ |
@@ -460,7 +460,7 @@ discard block |
||
| 460 | 460 | * find multiple matches based on a single field and match criteria |
| 461 | 461 | * |
| 462 | 462 | * @param string $fieldname |
| 463 | - * @param mixed $match string|array |
|
| 463 | + * @param string $match string|array |
|
| 464 | 464 | * @return object[] of objects of calling class |
| 465 | 465 | */ |
| 466 | 466 | public static function fetchAllWhereMatchingSingleField( $fieldname, $match ) { |
@@ -484,7 +484,7 @@ discard block |
||
| 484 | 484 | /** |
| 485 | 485 | * returns number of rows in the table |
| 486 | 486 | * |
| 487 | - * @return int |
|
| 487 | + * @return string |
|
| 488 | 488 | */ |
| 489 | 489 | static public function count() { |
| 490 | 490 | $st = static::execute( 'SELECT COUNT(*) FROM ' . static::_quote_identifier( static::$_tableName ) ); |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | * |
| 497 | 497 | * @param string $SQLfragment conditions, grouping to apply (to right of WHERE keyword) |
| 498 | 498 | * @param array $params optional params to be escaped and injected into the SQL query (standrd PDO syntax) |
| 499 | - * @return integer count of rows matching conditions |
|
| 499 | + * @return string count of rows matching conditions |
|
| 500 | 500 | */ |
| 501 | 501 | static public function countAllWhere( $SQLfragment = '', $params = array() ) { |
| 502 | 502 | $SQLfragment = self::addWherePrefix( $SQLfragment ); |
@@ -19,7 +19,6 @@ |
||
| 19 | 19 | * |
| 20 | 20 | * @package default |
| 21 | 21 | */ |
| 22 | -use phpDocumentor\Reflection\Types\Object_; |
|
| 23 | 22 | |
| 24 | 23 | /** |
| 25 | 24 | * Class Model |
@@ -77,11 +77,11 @@ discard block |
||
| 77 | 77 | * Model constructor. |
| 78 | 78 | * @param array $data |
| 79 | 79 | */ |
| 80 | - public function __construct( $data = array() ) { |
|
| 80 | + public function __construct($data = array()) { |
|
| 81 | 81 | static::getFieldnames(); // only called once first time an object is created |
| 82 | 82 | $this->clearDirtyFields(); |
| 83 | - if (is_array( $data )) { |
|
| 84 | - $this->hydrate( $data ); |
|
| 83 | + if (is_array($data)) { |
|
| 84 | + $this->hydrate($data); |
|
| 85 | 85 | } |
| 86 | 86 | } |
| 87 | 87 | |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * @return bool |
| 92 | 92 | */ |
| 93 | 93 | public function hasData() { |
| 94 | - return is_object( $this->data ); |
|
| 94 | + return is_object($this->data); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | public function dataPresent() { |
| 105 | 105 | if (!$this->hasData()) { |
| 106 | - throw new \Exception( 'No data' ); |
|
| 106 | + throw new \Exception('No data'); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | return true; |
@@ -118,12 +118,12 @@ discard block |
||
| 118 | 118 | * |
| 119 | 119 | * @return void |
| 120 | 120 | */ |
| 121 | - public function __set( $name, $value ) { |
|
| 121 | + public function __set($name, $value) { |
|
| 122 | 122 | if (!$this->hasData()) { |
| 123 | 123 | $this->data = new \stdClass(); |
| 124 | 124 | } |
| 125 | 125 | $this->data->$name = $value; |
| 126 | - $this->markFieldDirty( $name ); |
|
| 126 | + $this->markFieldDirty($name); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @param $name |
| 133 | 133 | */ |
| 134 | - public function markFieldDirty( $name ) { |
|
| 134 | + public function markFieldDirty($name) { |
|
| 135 | 135 | $this->dirty->$name = true; // field became dirty |
| 136 | 136 | } |
| 137 | 137 | |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | * @param $name |
| 142 | 142 | * @return bool |
| 143 | 143 | */ |
| 144 | - public function isFieldDirty( $name ) { |
|
| 144 | + public function isFieldDirty($name) { |
|
| 145 | 145 | return isset($this->dirty->$name) && ($this->dirty->$name == true); |
| 146 | 146 | } |
| 147 | 147 | |
@@ -161,20 +161,20 @@ discard block |
||
| 161 | 161 | * @return mixed |
| 162 | 162 | * @throws \Exception |
| 163 | 163 | */ |
| 164 | - public function __get( $name ) { |
|
| 164 | + public function __get($name) { |
|
| 165 | 165 | if (!$this->hasData()) { |
| 166 | - throw new \Exception( "data property=$name has not been initialised", 1 ); |
|
| 166 | + throw new \Exception("data property=$name has not been initialised", 1); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - if (property_exists( $this->data, $name )) { |
|
| 169 | + if (property_exists($this->data, $name)) { |
|
| 170 | 170 | return $this->data->$name; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | $trace = debug_backtrace(); |
| 174 | 174 | throw new \Exception( |
| 175 | - 'Undefined property via __get(): ' . $name . |
|
| 176 | - ' in ' . $trace[0]['file'] . |
|
| 177 | - ' on line ' . $trace[0]['line'], |
|
| 175 | + 'Undefined property via __get(): '.$name. |
|
| 176 | + ' in '.$trace[0]['file']. |
|
| 177 | + ' on line '.$trace[0]['line'], |
|
| 178 | 178 | 1 |
| 179 | 179 | ); |
| 180 | 180 | } |
@@ -187,8 +187,8 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return bool |
| 189 | 189 | */ |
| 190 | - public function __isset( $name ) { |
|
| 191 | - if ($this->hasData() && property_exists( $this->data, $name )) { |
|
| 190 | + public function __isset($name) { |
|
| 191 | + if ($this->hasData() && property_exists($this->data, $name)) { |
|
| 192 | 192 | return true; |
| 193 | 193 | } |
| 194 | 194 | |
@@ -206,9 +206,9 @@ discard block |
||
| 206 | 206 | * @param string $password |
| 207 | 207 | * @param array $driverOptions |
| 208 | 208 | */ |
| 209 | - public static function connectDb( $dsn, $username, $password, $driverOptions = array() ) { |
|
| 210 | - static::$_db = new \PDO( $dsn, $username, $password, $driverOptions ); |
|
| 211 | - static::$_db->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION ); // Set Errorhandling to Exception |
|
| 209 | + public static function connectDb($dsn, $username, $password, $driverOptions = array()) { |
|
| 210 | + static::$_db = new \PDO($dsn, $username, $password, $driverOptions); |
|
| 211 | + static::$_db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // Set Errorhandling to Exception |
|
| 212 | 212 | static::_setup_identifier_quote_character(); |
| 213 | 213 | } |
| 214 | 214 | |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | * @return void |
| 220 | 220 | */ |
| 221 | 221 | public static function _setup_identifier_quote_character() { |
| 222 | - if (is_null( static::$_identifier_quote_character )) { |
|
| 222 | + if (is_null(static::$_identifier_quote_character)) { |
|
| 223 | 223 | static::$_identifier_quote_character = static::_detect_identifier_quote_character(); |
| 224 | 224 | } |
| 225 | 225 | } |
@@ -253,9 +253,9 @@ discard block |
||
| 253 | 253 | */ |
| 254 | 254 | protected static function getDriverName() { |
| 255 | 255 | if (!static::$_db) { |
| 256 | - throw new \Exception( 'No database connection setup' ); |
|
| 256 | + throw new \Exception('No database connection setup'); |
|
| 257 | 257 | } |
| 258 | - return static::$_db->getAttribute( \PDO::ATTR_DRIVER_NAME ); |
|
| 258 | + return static::$_db->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
| 259 | 259 | } |
| 260 | 260 | |
| 261 | 261 | /** |
@@ -266,14 +266,14 @@ discard block |
||
| 266 | 266 | * @param string $identifier |
| 267 | 267 | * @return string |
| 268 | 268 | */ |
| 269 | - protected static function _quote_identifier( $identifier ) { |
|
| 269 | + protected static function _quote_identifier($identifier) { |
|
| 270 | 270 | $class = get_called_class(); |
| 271 | - $parts = explode( '.', $identifier ); |
|
| 272 | - $parts = array_map( array( |
|
| 271 | + $parts = explode('.', $identifier); |
|
| 272 | + $parts = array_map(array( |
|
| 273 | 273 | $class, |
| 274 | 274 | '_quote_identifier_part' |
| 275 | - ), $parts ); |
|
| 276 | - return join( '.', $parts ); |
|
| 275 | + ), $parts); |
|
| 276 | + return join('.', $parts); |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | |
@@ -285,11 +285,11 @@ discard block |
||
| 285 | 285 | * @param string $part |
| 286 | 286 | * @return string |
| 287 | 287 | */ |
| 288 | - protected static function _quote_identifier_part( $part ) { |
|
| 288 | + protected static function _quote_identifier_part($part) { |
|
| 289 | 289 | if ($part === '*') { |
| 290 | 290 | return $part; |
| 291 | 291 | } |
| 292 | - return static::$_identifier_quote_character . $part . static::$_identifier_quote_character; |
|
| 292 | + return static::$_identifier_quote_character.$part.static::$_identifier_quote_character; |
|
| 293 | 293 | } |
| 294 | 294 | |
| 295 | 295 | /** |
@@ -300,8 +300,8 @@ discard block |
||
| 300 | 300 | protected static function getFieldnames() { |
| 301 | 301 | $class = get_called_class(); |
| 302 | 302 | if (!isset(self::$_tableColumns[$class])) { |
| 303 | - $st = static::execute( 'DESCRIBE ' . static::_quote_identifier( static::$_tableName ) ); |
|
| 304 | - self::$_tableColumns[$class] = $st->fetchAll( \PDO::FETCH_COLUMN ); |
|
| 303 | + $st = static::execute('DESCRIBE '.static::_quote_identifier(static::$_tableName)); |
|
| 304 | + self::$_tableColumns[$class] = $st->fetchAll(\PDO::FETCH_COLUMN); |
|
| 305 | 305 | } |
| 306 | 306 | return self::$_tableColumns[$class]; |
| 307 | 307 | } |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | * |
| 314 | 314 | * @return void |
| 315 | 315 | */ |
| 316 | - public function hydrate( $data ) { |
|
| 316 | + public function hydrate($data) { |
|
| 317 | 317 | foreach (static::getFieldnames() as $fieldname) { |
| 318 | 318 | if (isset($data[$fieldname])) { |
| 319 | 319 | $this->$fieldname = $data[$fieldname]; |
@@ -359,8 +359,8 @@ discard block |
||
| 359 | 359 | * @param string $id |
| 360 | 360 | * @return Object |
| 361 | 361 | */ |
| 362 | - static public function getById( $id ) { |
|
| 363 | - return static::fetchOneWhere( static::_quote_identifier( static::$_primary_column_name ) . ' = ?', array($id) ); |
|
| 362 | + static public function getById($id) { |
|
| 363 | + return static::fetchOneWhere(static::_quote_identifier(static::$_primary_column_name).' = ?', array($id)); |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | /** |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | * @return Object |
| 370 | 370 | */ |
| 371 | 371 | static public function first() { |
| 372 | - return static::fetchOneWhere( '1=1 ORDER BY ' . static::_quote_identifier( static::$_primary_column_name ) . ' ASC' ); |
|
| 372 | + return static::fetchOneWhere('1=1 ORDER BY '.static::_quote_identifier(static::$_primary_column_name).' ASC'); |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | /** |
@@ -378,7 +378,7 @@ discard block |
||
| 378 | 378 | * @return Object |
| 379 | 379 | */ |
| 380 | 380 | static public function last() { |
| 381 | - return static::fetchOneWhere( '1=1 ORDER BY ' . static::_quote_identifier( static::$_primary_column_name ) . ' DESC' ); |
|
| 381 | + return static::fetchOneWhere('1=1 ORDER BY '.static::_quote_identifier(static::$_primary_column_name).' DESC'); |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -387,9 +387,9 @@ discard block |
||
| 387 | 387 | * @param string $id |
| 388 | 388 | * @return object[] of objects for matching records |
| 389 | 389 | */ |
| 390 | - static public function find( $id ) { |
|
| 391 | - $find_by_method = 'find_by_' . (static::$_primary_column_name); |
|
| 392 | - static::$find_by_method( $id ); |
|
| 390 | + static public function find($id) { |
|
| 391 | + $find_by_method = 'find_by_'.(static::$_primary_column_name); |
|
| 392 | + static::$find_by_method($id); |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | /** |
@@ -404,39 +404,39 @@ discard block |
||
| 404 | 404 | * @return mixed int|object[]|object |
| 405 | 405 | * @throws \Exception |
| 406 | 406 | */ |
| 407 | - static public function __callStatic( $name, $arguments ) { |
|
| 407 | + static public function __callStatic($name, $arguments) { |
|
| 408 | 408 | // Note: value of $name is case sensitive. |
| 409 | - if (preg_match( '/^find_by_/', $name ) == 1) { |
|
| 409 | + if (preg_match('/^find_by_/', $name) == 1) { |
|
| 410 | 410 | // it's a find_by_{fieldname} dynamic method |
| 411 | - $fieldname = substr( $name, 8 ); // remove find by |
|
| 411 | + $fieldname = substr($name, 8); // remove find by |
|
| 412 | 412 | $match = $arguments[0]; |
| 413 | - return static::fetchAllWhereMatchingSingleField( $fieldname, $match ); |
|
| 414 | - } else if (preg_match( '/^findOne_by_/', $name ) == 1) { |
|
| 413 | + return static::fetchAllWhereMatchingSingleField($fieldname, $match); |
|
| 414 | + } else if (preg_match('/^findOne_by_/', $name) == 1) { |
|
| 415 | 415 | // it's a findOne_by_{fieldname} dynamic method |
| 416 | - $fieldname = substr( $name, 11 ); // remove findOne_by_ |
|
| 416 | + $fieldname = substr($name, 11); // remove findOne_by_ |
|
| 417 | 417 | $match = $arguments[0]; |
| 418 | - return static::fetchOneWhereMatchingSingleField( $fieldname, $match, 'ASC' ); |
|
| 419 | - } else if (preg_match( '/^first_by_/', $name ) == 1) { |
|
| 418 | + return static::fetchOneWhereMatchingSingleField($fieldname, $match, 'ASC'); |
|
| 419 | + } else if (preg_match('/^first_by_/', $name) == 1) { |
|
| 420 | 420 | // it's a first_by_{fieldname} dynamic method |
| 421 | - $fieldname = substr( $name, 9 ); // remove first_by_ |
|
| 421 | + $fieldname = substr($name, 9); // remove first_by_ |
|
| 422 | 422 | $match = $arguments[0]; |
| 423 | - return static::fetchOneWhereMatchingSingleField( $fieldname, $match, 'ASC' ); |
|
| 424 | - } else if (preg_match( '/^last_by_/', $name ) == 1) { |
|
| 423 | + return static::fetchOneWhereMatchingSingleField($fieldname, $match, 'ASC'); |
|
| 424 | + } else if (preg_match('/^last_by_/', $name) == 1) { |
|
| 425 | 425 | // it's a last_by_{fieldname} dynamic method |
| 426 | - $fieldname = substr( $name, 8 ); // remove last_by_ |
|
| 426 | + $fieldname = substr($name, 8); // remove last_by_ |
|
| 427 | 427 | $match = $arguments[0]; |
| 428 | - return static::fetchOneWhereMatchingSingleField( $fieldname, $match, 'DESC' ); |
|
| 429 | - } else if (preg_match( '/^count_by_/', $name ) == 1) { |
|
| 428 | + return static::fetchOneWhereMatchingSingleField($fieldname, $match, 'DESC'); |
|
| 429 | + } else if (preg_match('/^count_by_/', $name) == 1) { |
|
| 430 | 430 | // it's a count_by_{fieldname} dynamic method |
| 431 | - $fieldname = substr( $name, 9 ); // remove find by |
|
| 431 | + $fieldname = substr($name, 9); // remove find by |
|
| 432 | 432 | $match = $arguments[0]; |
| 433 | - if (is_array( $match )) { |
|
| 434 | - return static::countAllWhere( static::_quote_identifier( $fieldname ) . ' IN (' . static::createInClausePlaceholders( $match ) . ')', $match ); |
|
| 433 | + if (is_array($match)) { |
|
| 434 | + return static::countAllWhere(static::_quote_identifier($fieldname).' IN ('.static::createInClausePlaceholders($match).')', $match); |
|
| 435 | 435 | } else { |
| 436 | - return static::countAllWhere( static::_quote_identifier( $fieldname ) . ' = ?', array($match) ); |
|
| 436 | + return static::countAllWhere(static::_quote_identifier($fieldname).' = ?', array($match)); |
|
| 437 | 437 | } |
| 438 | 438 | } |
| 439 | - throw new \Exception( __CLASS__ . ' not such static method[' . $name . ']' ); |
|
| 439 | + throw new \Exception(__CLASS__.' not such static method['.$name.']'); |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | /** |
@@ -447,11 +447,11 @@ discard block |
||
| 447 | 447 | * @param string $order ASC|DESC |
| 448 | 448 | * @return object of calling class |
| 449 | 449 | */ |
| 450 | - public static function fetchOneWhereMatchingSingleField( $fieldname, $match, $order ) { |
|
| 451 | - if (is_array( $match )) { |
|
| 452 | - return static::fetchOneWhere( static::_quote_identifier( $fieldname ) . ' IN (' . static::createInClausePlaceholders( $match ) . ') ORDER BY ' . static::_quote_identifier( $fieldname ) . ' ' . $order, $match ); |
|
| 450 | + public static function fetchOneWhereMatchingSingleField($fieldname, $match, $order) { |
|
| 451 | + if (is_array($match)) { |
|
| 452 | + return static::fetchOneWhere(static::_quote_identifier($fieldname).' IN ('.static::createInClausePlaceholders($match).') ORDER BY '.static::_quote_identifier($fieldname).' '.$order, $match); |
|
| 453 | 453 | } else { |
| 454 | - return static::fetchOneWhere( static::_quote_identifier( $fieldname ) . ' = ? ORDER BY ' . static::_quote_identifier( $fieldname ) . ' ' . $order, array($match) ); |
|
| 454 | + return static::fetchOneWhere(static::_quote_identifier($fieldname).' = ? ORDER BY '.static::_quote_identifier($fieldname).' '.$order, array($match)); |
|
| 455 | 455 | } |
| 456 | 456 | } |
| 457 | 457 | |
@@ -463,11 +463,11 @@ discard block |
||
| 463 | 463 | * @param mixed $match string|array |
| 464 | 464 | * @return object[] of objects of calling class |
| 465 | 465 | */ |
| 466 | - public static function fetchAllWhereMatchingSingleField( $fieldname, $match ) { |
|
| 467 | - if (is_array( $match )) { |
|
| 468 | - return static::fetchAllWhere( static::_quote_identifier( $fieldname ) . ' IN (' . static::createInClausePlaceholders( $match ) . ')', $match ); |
|
| 466 | + public static function fetchAllWhereMatchingSingleField($fieldname, $match) { |
|
| 467 | + if (is_array($match)) { |
|
| 468 | + return static::fetchAllWhere(static::_quote_identifier($fieldname).' IN ('.static::createInClausePlaceholders($match).')', $match); |
|
| 469 | 469 | } else { |
| 470 | - return static::fetchAllWhere( static::_quote_identifier( $fieldname ) . ' = ?', array($match) ); |
|
| 470 | + return static::fetchAllWhere(static::_quote_identifier($fieldname).' = ?', array($match)); |
|
| 471 | 471 | } |
| 472 | 472 | } |
| 473 | 473 | |
@@ -477,8 +477,8 @@ discard block |
||
| 477 | 477 | * @param array $params |
| 478 | 478 | * @return string |
| 479 | 479 | */ |
| 480 | - static public function createInClausePlaceholders( $params ) { |
|
| 481 | - return implode( ',', array_fill( 0, count( $params ), '?' ) ); |
|
| 480 | + static public function createInClausePlaceholders($params) { |
|
| 481 | + return implode(',', array_fill(0, count($params), '?')); |
|
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | /** |
@@ -487,7 +487,7 @@ discard block |
||
| 487 | 487 | * @return int |
| 488 | 488 | */ |
| 489 | 489 | static public function count() { |
| 490 | - $st = static::execute( 'SELECT COUNT(*) FROM ' . static::_quote_identifier( static::$_tableName ) ); |
|
| 490 | + $st = static::execute('SELECT COUNT(*) FROM '.static::_quote_identifier(static::$_tableName)); |
|
| 491 | 491 | return $st->fetchColumn(); |
| 492 | 492 | } |
| 493 | 493 | |
@@ -498,9 +498,9 @@ discard block |
||
| 498 | 498 | * @param array $params optional params to be escaped and injected into the SQL query (standrd PDO syntax) |
| 499 | 499 | * @return integer count of rows matching conditions |
| 500 | 500 | */ |
| 501 | - static public function countAllWhere( $SQLfragment = '', $params = array() ) { |
|
| 502 | - $SQLfragment = self::addWherePrefix( $SQLfragment ); |
|
| 503 | - $st = static::execute( 'SELECT COUNT(*) FROM ' . static::_quote_identifier( static::$_tableName ) . $SQLfragment, $params ); |
|
| 501 | + static public function countAllWhere($SQLfragment = '', $params = array()) { |
|
| 502 | + $SQLfragment = self::addWherePrefix($SQLfragment); |
|
| 503 | + $st = static::execute('SELECT COUNT(*) FROM '.static::_quote_identifier(static::$_tableName).$SQLfragment, $params); |
|
| 504 | 504 | return $st->fetchColumn(); |
| 505 | 505 | } |
| 506 | 506 | |
@@ -510,8 +510,8 @@ discard block |
||
| 510 | 510 | * @param string $SQLfragment |
| 511 | 511 | * @return string |
| 512 | 512 | */ |
| 513 | - static protected function addWherePrefix( $SQLfragment ) { |
|
| 514 | - return $SQLfragment ? ' WHERE ' . $SQLfragment : $SQLfragment; |
|
| 513 | + static protected function addWherePrefix($SQLfragment) { |
|
| 514 | + return $SQLfragment ? ' WHERE '.$SQLfragment : $SQLfragment; |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | |
@@ -523,20 +523,20 @@ discard block |
||
| 523 | 523 | * @param bool $limitOne if true the first match will be returned |
| 524 | 524 | * @return mixed object[]|object of objects of calling class |
| 525 | 525 | */ |
| 526 | - static public function fetchWhere( $SQLfragment = '', $params = array(), $limitOne = false ) { |
|
| 526 | + static public function fetchWhere($SQLfragment = '', $params = array(), $limitOne = false) { |
|
| 527 | 527 | $class = get_called_class(); |
| 528 | - $SQLfragment = self::addWherePrefix( $SQLfragment ); |
|
| 528 | + $SQLfragment = self::addWherePrefix($SQLfragment); |
|
| 529 | 529 | $st = static::execute( |
| 530 | - 'SELECT * FROM ' . static::_quote_identifier( static::$_tableName ) . $SQLfragment . ($limitOne ? ' LIMIT 1' : ''), |
|
| 530 | + 'SELECT * FROM '.static::_quote_identifier(static::$_tableName).$SQLfragment.($limitOne ? ' LIMIT 1' : ''), |
|
| 531 | 531 | $params |
| 532 | 532 | ); |
| 533 | - $st->setFetchMode( \PDO::FETCH_ASSOC ); |
|
| 533 | + $st->setFetchMode(\PDO::FETCH_ASSOC); |
|
| 534 | 534 | if ($limitOne) { |
| 535 | - return new $class( $st->fetch() ); |
|
| 535 | + return new $class($st->fetch()); |
|
| 536 | 536 | } |
| 537 | 537 | $results = []; |
| 538 | 538 | foreach ($st->fetch() as $row) { |
| 539 | - $results[] = new $class( $row ); |
|
| 539 | + $results[] = new $class($row); |
|
| 540 | 540 | } |
| 541 | 541 | return $results; |
| 542 | 542 | } |
@@ -548,8 +548,8 @@ discard block |
||
| 548 | 548 | * @param array $params optional params to be escaped and injected into the SQL query (standrd PDO syntax) |
| 549 | 549 | * @return object[] of objects of calling class |
| 550 | 550 | */ |
| 551 | - static public function fetchAllWhere( $SQLfragment = '', $params = array() ) { |
|
| 552 | - return static::fetchWhere( $SQLfragment, $params, false ); |
|
| 551 | + static public function fetchAllWhere($SQLfragment = '', $params = array()) { |
|
| 552 | + return static::fetchWhere($SQLfragment, $params, false); |
|
| 553 | 553 | } |
| 554 | 554 | |
| 555 | 555 | /** |
@@ -559,8 +559,8 @@ discard block |
||
| 559 | 559 | * @param array $params optional params to be escaped and injected into the SQL query (standrd PDO syntax) |
| 560 | 560 | * @return object of calling class |
| 561 | 561 | */ |
| 562 | - static public function fetchOneWhere( $SQLfragment = '', $params = array() ) { |
|
| 563 | - return static::fetchWhere( $SQLfragment, $params, true ); |
|
| 562 | + static public function fetchOneWhere($SQLfragment = '', $params = array()) { |
|
| 563 | + return static::fetchWhere($SQLfragment, $params, true); |
|
| 564 | 564 | } |
| 565 | 565 | |
| 566 | 566 | /** |
@@ -568,9 +568,9 @@ discard block |
||
| 568 | 568 | * |
| 569 | 569 | * @return boolean indicating success |
| 570 | 570 | */ |
| 571 | - static public function deleteById( $id ) { |
|
| 571 | + static public function deleteById($id) { |
|
| 572 | 572 | $st = static::execute( |
| 573 | - 'DELETE FROM ' . static::_quote_identifier( static::$_tableName ) . ' WHERE ' . static::_quote_identifier( static::$_primary_column_name ) . ' = ? LIMIT 1', |
|
| 573 | + 'DELETE FROM '.static::_quote_identifier(static::$_tableName).' WHERE '.static::_quote_identifier(static::$_primary_column_name).' = ? LIMIT 1', |
|
| 574 | 574 | array($id) |
| 575 | 575 | ); |
| 576 | 576 | return ($st->rowCount() == 1); |
@@ -582,7 +582,7 @@ discard block |
||
| 582 | 582 | * @return boolean indicating success |
| 583 | 583 | */ |
| 584 | 584 | public function delete() { |
| 585 | - return self::deleteById( $this->{static::$_primary_column_name} ); |
|
| 585 | + return self::deleteById($this->{static::$_primary_column_name} ); |
|
| 586 | 586 | } |
| 587 | 587 | |
| 588 | 588 | /** |
@@ -592,9 +592,9 @@ discard block |
||
| 592 | 592 | * @param array $params optional params to be escaped and injected into the SQL query (standrd PDO syntax) |
| 593 | 593 | * @return \PDOStatement |
| 594 | 594 | */ |
| 595 | - static public function deleteAllWhere( $where, $params = array() ) { |
|
| 595 | + static public function deleteAllWhere($where, $params = array()) { |
|
| 596 | 596 | $st = static::execute( |
| 597 | - 'DELETE FROM ' . static::_quote_identifier( static::$_tableName ) . ' WHERE ' . $where, |
|
| 597 | + 'DELETE FROM '.static::_quote_identifier(static::$_tableName).' WHERE '.$where, |
|
| 598 | 598 | $params |
| 599 | 599 | ); |
| 600 | 600 | return $st; |
@@ -617,22 +617,22 @@ discard block |
||
| 617 | 617 | * @param boolean $allowSetPrimaryKey if true include primary key field in insert (ie. you want to set it yourself) |
| 618 | 618 | * @return boolean indicating success |
| 619 | 619 | */ |
| 620 | - public function insert( $autoTimestamp = true, $allowSetPrimaryKey = false ) { |
|
| 620 | + public function insert($autoTimestamp = true, $allowSetPrimaryKey = false) { |
|
| 621 | 621 | $pk = static::$_primary_column_name; |
| 622 | - $timeStr = gmdate( 'Y-m-d H:i:s' ); |
|
| 623 | - if ($autoTimestamp && in_array( 'created_at', static::getFieldnames() )) { |
|
| 622 | + $timeStr = gmdate('Y-m-d H:i:s'); |
|
| 623 | + if ($autoTimestamp && in_array('created_at', static::getFieldnames())) { |
|
| 624 | 624 | $this->created_at = $timeStr; |
| 625 | 625 | } |
| 626 | - if ($autoTimestamp && in_array( 'updated_at', static::getFieldnames() )) { |
|
| 626 | + if ($autoTimestamp && in_array('updated_at', static::getFieldnames())) { |
|
| 627 | 627 | $this->updated_at = $timeStr; |
| 628 | 628 | } |
| 629 | 629 | $this->validate(); |
| 630 | 630 | if ($allowSetPrimaryKey !== true) { |
| 631 | 631 | $this->$pk = null; // ensure id is null |
| 632 | 632 | } |
| 633 | - $set = $this->setString( !$allowSetPrimaryKey ); |
|
| 634 | - $query = 'INSERT INTO ' . static::_quote_identifier( static::$_tableName ) . ' SET ' . $set['sql']; |
|
| 635 | - $st = static::execute( $query, $set['params'] ); |
|
| 633 | + $set = $this->setString(!$allowSetPrimaryKey); |
|
| 634 | + $query = 'INSERT INTO '.static::_quote_identifier(static::$_tableName).' SET '.$set['sql']; |
|
| 635 | + $st = static::execute($query, $set['params']); |
|
| 636 | 636 | if ($st->rowCount() == 1) { |
| 637 | 637 | $this->{static::$_primary_column_name} = static::$_db->lastInsertId(); |
| 638 | 638 | $this->clearDirtyFields(); |
@@ -646,13 +646,13 @@ discard block |
||
| 646 | 646 | * @param boolean $autoTimestamp true by default will set updated_at field if present |
| 647 | 647 | * @return boolean indicating success |
| 648 | 648 | */ |
| 649 | - public function update( $autoTimestamp = true ) { |
|
| 650 | - if ($autoTimestamp && in_array( 'updated_at', static::getFieldnames() )) { |
|
| 651 | - $this->updated_at = gmdate( 'Y-m-d H:i:s' ); |
|
| 649 | + public function update($autoTimestamp = true) { |
|
| 650 | + if ($autoTimestamp && in_array('updated_at', static::getFieldnames())) { |
|
| 651 | + $this->updated_at = gmdate('Y-m-d H:i:s'); |
|
| 652 | 652 | } |
| 653 | 653 | $this->validate(); |
| 654 | 654 | $set = $this->setString(); |
| 655 | - $query = 'UPDATE ' . static::_quote_identifier( static::$_tableName ) . ' SET ' . $set['sql'] . ' WHERE ' . static::_quote_identifier( static::$_primary_column_name ) . ' = ? LIMIT 1'; |
|
| 655 | + $query = 'UPDATE '.static::_quote_identifier(static::$_tableName).' SET '.$set['sql'].' WHERE '.static::_quote_identifier(static::$_primary_column_name).' = ? LIMIT 1'; |
|
| 656 | 656 | $set['params'][] = $this->{static::$_primary_column_name}; |
| 657 | 657 | $st = static::execute( |
| 658 | 658 | $query, |
@@ -673,9 +673,9 @@ discard block |
||
| 673 | 673 | * @param array $params array of parameters to replace the placeholders in the statement |
| 674 | 674 | * @return \PDOStatement handle |
| 675 | 675 | */ |
| 676 | - public static function execute( $query, $params = array() ) { |
|
| 677 | - $st = static::_prepare( $query ); |
|
| 678 | - $st->execute( $params ); |
|
| 676 | + public static function execute($query, $params = array()) { |
|
| 677 | + $st = static::_prepare($query); |
|
| 678 | + $st->execute($params); |
|
| 679 | 679 | return $st; |
| 680 | 680 | } |
| 681 | 681 | |
@@ -685,10 +685,10 @@ discard block |
||
| 685 | 685 | * @param string $query |
| 686 | 686 | * @return \PDOStatement |
| 687 | 687 | */ |
| 688 | - protected static function _prepare( $query ) { |
|
| 688 | + protected static function _prepare($query) { |
|
| 689 | 689 | if (!isset(static::$_stmt[$query])) { |
| 690 | 690 | // cache prepared query if not seen before |
| 691 | - static::$_stmt[$query] = static::$_db->prepare( $query ); |
|
| 691 | + static::$_stmt[$query] = static::$_db->prepare($query); |
|
| 692 | 692 | } |
| 693 | 693 | return static::$_stmt[$query]; // return cache copy |
| 694 | 694 | } |
@@ -714,7 +714,7 @@ discard block |
||
| 714 | 714 | * @param boolean $ignorePrimary |
| 715 | 715 | * @return string[string] ['sql' => string, 'params' => mixed[] ] |
| 716 | 716 | */ |
| 717 | - protected function setString( $ignorePrimary = true ) { |
|
| 717 | + protected function setString($ignorePrimary = true) { |
|
| 718 | 718 | // escapes and builds mysql SET string returning false, empty string or `field` = 'val'[, `field` = 'val']... |
| 719 | 719 | /** |
| 720 | 720 | * @var array $fragments individual SQL assignments |
@@ -728,18 +728,18 @@ discard block |
||
| 728 | 728 | if ($ignorePrimary && $field == static::$_primary_column_name) { |
| 729 | 729 | continue; |
| 730 | 730 | } |
| 731 | - if (isset($this->$field) && $this->isFieldDirty( $field )) { // Only if dirty |
|
| 731 | + if (isset($this->$field) && $this->isFieldDirty($field)) { // Only if dirty |
|
| 732 | 732 | if ($this->$field === null) { |
| 733 | 733 | // if empty set to NULL |
| 734 | - $fragments[] = static::_quote_identifier( $field ) . ' = NULL'; |
|
| 734 | + $fragments[] = static::_quote_identifier($field).' = NULL'; |
|
| 735 | 735 | } else { |
| 736 | 736 | // Just set value normally as not empty string with NULL allowed |
| 737 | - $fragments[] = static::_quote_identifier( $field ) . ' = ?'; |
|
| 737 | + $fragments[] = static::_quote_identifier($field).' = ?'; |
|
| 738 | 738 | $params[] = $this->$field; |
| 739 | 739 | } |
| 740 | 740 | } |
| 741 | 741 | } |
| 742 | - $sqlFragment = implode( ", ", $fragments ); |
|
| 742 | + $sqlFragment = implode(", ", $fragments); |
|
| 743 | 743 | return [ |
| 744 | 744 | 'sql' => $sqlFragment, |
| 745 | 745 | 'params' => $params |
@@ -752,9 +752,9 @@ discard block |
||
| 752 | 752 | * @param string|int $dt a date string or a unix timestamp |
| 753 | 753 | * @return string |
| 754 | 754 | */ |
| 755 | - public static function datetimeToMysqldatetime( $dt ) { |
|
| 756 | - $dt = (is_string( $dt )) ? strtotime( $dt ) : $dt; |
|
| 757 | - return date( 'Y-m-d H:i:s', $dt ); |
|
| 755 | + public static function datetimeToMysqldatetime($dt) { |
|
| 756 | + $dt = (is_string($dt)) ? strtotime($dt) : $dt; |
|
| 757 | + return date('Y-m-d H:i:s', $dt); |
|
| 758 | 758 | } |
| 759 | 759 | } |
| 760 | 760 | |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | public static function setUpBeforeClass() { |
| 14 | 14 | // connect and setup db categorytest with table categories |
| 15 | 15 | try { |
| 16 | - Freshsauce\Model\Model::connectDb( 'mysql:host=127.0.0.1;dbname=', 'root', '' ); |
|
| 16 | + Freshsauce\Model\Model::connectDb('mysql:host=127.0.0.1;dbname=', 'root', ''); |
|
| 17 | 17 | } catch (PDOException $e) { |
| 18 | 18 | if ($e->getCode() != 0) { |
| 19 | 19 | // throw it on |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | ]; |
| 36 | 36 | |
| 37 | 37 | foreach ($sql_setup as $sql) { |
| 38 | - Freshsauce\Model\Model::execute( $sql ); |
|
| 38 | + Freshsauce\Model\Model::execute($sql); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | } |
@@ -52,16 +52,16 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function testCreate() { |
| 54 | 54 | $_name = 'Fiction'; |
| 55 | - $category = new App\Model\Category( array( |
|
| 55 | + $category = new App\Model\Category(array( |
|
| 56 | 56 | 'name' => $_name |
| 57 | - ) ); |
|
| 57 | + )); |
|
| 58 | 58 | |
| 59 | 59 | $category->save(); // no Id so will insert |
| 60 | 60 | |
| 61 | - $this->assertEquals( $category->name, $_name ); |
|
| 62 | - $this->assertNotEmpty( $category->id ); |
|
| 63 | - $this->assertNotEmpty( $category->created_at ); |
|
| 64 | - $this->assertNotEmpty( $category->updated_at ); |
|
| 61 | + $this->assertEquals($category->name, $_name); |
|
| 62 | + $this->assertNotEmpty($category->id); |
|
| 63 | + $this->assertNotEmpty($category->created_at); |
|
| 64 | + $this->assertNotEmpty($category->updated_at); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
@@ -69,21 +69,21 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function testCreateAndGetById() { |
| 71 | 71 | $_name = 'SciFi'; |
| 72 | - $category = new App\Model\Category( array( |
|
| 72 | + $category = new App\Model\Category(array( |
|
| 73 | 73 | 'name' => $_name |
| 74 | - ) ); |
|
| 74 | + )); |
|
| 75 | 75 | |
| 76 | 76 | $category->save(); // no Id so will insert |
| 77 | 77 | |
| 78 | - $this->assertEquals( $category->name, $_name ); |
|
| 79 | - $this->assertNotEmpty( $category->id ); |
|
| 78 | + $this->assertEquals($category->name, $_name); |
|
| 79 | + $this->assertNotEmpty($category->id); |
|
| 80 | 80 | |
| 81 | 81 | // read category back into a new object |
| 82 | - $read_category = App\Model\Category::getById( $category->id ); |
|
| 83 | - $this->assertEquals( $read_category->name, $_name ); |
|
| 84 | - $this->assertEquals( $read_category->id, $category->id ); |
|
| 85 | - $this->assertNotEmpty( $category->created_at ); |
|
| 86 | - $this->assertNotEmpty( $category->updated_at ); |
|
| 82 | + $read_category = App\Model\Category::getById($category->id); |
|
| 83 | + $this->assertEquals($read_category->name, $_name); |
|
| 84 | + $this->assertEquals($read_category->id, $category->id); |
|
| 85 | + $this->assertNotEmpty($category->created_at); |
|
| 86 | + $this->assertNotEmpty($category->updated_at); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -91,28 +91,28 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function testCreateAndModify() { |
| 93 | 93 | $_name = 'Literature'; |
| 94 | - $category = new App\Model\Category( array( |
|
| 94 | + $category = new App\Model\Category(array( |
|
| 95 | 95 | 'name' => $_name |
| 96 | - ) ); |
|
| 96 | + )); |
|
| 97 | 97 | |
| 98 | 98 | $category->save(); // no Id so will insert |
| 99 | 99 | |
| 100 | - $this->assertEquals( $category->name, $_name ); |
|
| 101 | - $this->assertNotEmpty( $category->id ); |
|
| 102 | - $this->assertNotEmpty( $category->created_at ); |
|
| 103 | - $this->assertNotEmpty( $category->updated_at ); |
|
| 100 | + $this->assertEquals($category->name, $_name); |
|
| 101 | + $this->assertNotEmpty($category->id); |
|
| 102 | + $this->assertNotEmpty($category->created_at); |
|
| 103 | + $this->assertNotEmpty($category->updated_at); |
|
| 104 | 104 | |
| 105 | 105 | $_id = $category->id; |
| 106 | 106 | $_updated_at = $category->updated_at; |
| 107 | 107 | |
| 108 | 108 | $_new_name = 'Literature - great works'; |
| 109 | 109 | $category->name = $_new_name; |
| 110 | - sleep( 1 ); // to ensure updated_at time move forward a second at least |
|
| 110 | + sleep(1); // to ensure updated_at time move forward a second at least |
|
| 111 | 111 | $category->save(); |
| 112 | 112 | |
| 113 | - $this->assertEquals( $category->name, $_new_name ); |
|
| 114 | - $this->assertEquals( $category->id, $_id ); |
|
| 115 | - $this->assertNotEquals( $category->updated_at, $_updated_at ); |
|
| 113 | + $this->assertEquals($category->name, $_new_name); |
|
| 114 | + $this->assertEquals($category->id, $_id); |
|
| 115 | + $this->assertNotEquals($category->updated_at, $_updated_at); |
|
| 116 | 116 | |
| 117 | 117 | } |
| 118 | 118 | |
@@ -129,14 +129,14 @@ discard block |
||
| 129 | 129 | 'Cookbooks' |
| 130 | 130 | ]; |
| 131 | 131 | foreach ($_names as $_name) { |
| 132 | - $category = new App\Model\Category( array( |
|
| 132 | + $category = new App\Model\Category(array( |
|
| 133 | 133 | 'name' => $_name |
| 134 | - ) ); |
|
| 134 | + )); |
|
| 135 | 135 | $category->save(); // no Id so will insert |
| 136 | 136 | } |
| 137 | - $categories = App\Model\Category::find_by_name( $_names ); |
|
| 138 | - $this->assertNotEmpty( $categories ); |
|
| 139 | - $this->assertContainsOnlyInstancesOf( 'App\Model\Category', $categories ); |
|
| 140 | - $this->assertCount( count( $_names ), $categories ); |
|
| 137 | + $categories = App\Model\Category::find_by_name($_names); |
|
| 138 | + $this->assertNotEmpty($categories); |
|
| 139 | + $this->assertContainsOnlyInstancesOf('App\Model\Category', $categories); |
|
| 140 | + $this->assertCount(count($_names), $categories); |
|
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | \ No newline at end of file |