@@ -442,7 +442,7 @@ discard block |
||
442 | 442 | * find one match based on a single field and match criteria |
443 | 443 | * |
444 | 444 | * @param string $fieldname |
445 | - * @param string|array $match |
|
445 | + * @param string $match |
|
446 | 446 | * @param string $order ASC|DESC |
447 | 447 | * @return object of calling class |
448 | 448 | */ |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | * find multiple matches based on a single field and match criteria |
460 | 460 | * |
461 | 461 | * @param string $fieldname |
462 | - * @param string|array $match |
|
462 | + * @param string $match |
|
463 | 463 | * @return object[] of objects of calling class |
464 | 464 | */ |
465 | 465 | public static function fetchAllWhereMatchingSingleField( $fieldname, $match ) { |
@@ -76,11 +76,11 @@ discard block |
||
76 | 76 | * Model constructor. |
77 | 77 | * @param array $data |
78 | 78 | */ |
79 | - public function __construct( $data = array() ) { |
|
79 | + public function __construct($data = array()) { |
|
80 | 80 | static::getFieldnames(); // only called once first time an object is created |
81 | 81 | $this->clearDirtyFields(); |
82 | - if (is_array( $data )) { |
|
83 | - $this->hydrate( $data ); |
|
82 | + if (is_array($data)) { |
|
83 | + $this->hydrate($data); |
|
84 | 84 | } |
85 | 85 | } |
86 | 86 | |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * @return bool |
91 | 91 | */ |
92 | 92 | public function hasData() { |
93 | - return is_object( $this->data ); |
|
93 | + return is_object($this->data); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | */ |
103 | 103 | public function dataPresent() { |
104 | 104 | if (!$this->hasData()) { |
105 | - throw new \Exception( 'No data' ); |
|
105 | + throw new \Exception('No data'); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | return true; |
@@ -117,12 +117,12 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return void |
119 | 119 | */ |
120 | - public function __set( $name, $value ) { |
|
120 | + public function __set($name, $value) { |
|
121 | 121 | if (!$this->hasData()) { |
122 | 122 | $this->data = new \stdClass(); |
123 | 123 | } |
124 | 124 | $this->data->$name = $value; |
125 | - $this->markFieldDirty( $name ); |
|
125 | + $this->markFieldDirty($name); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * |
131 | 131 | * @param string $name |
132 | 132 | */ |
133 | - public function markFieldDirty( $name ) { |
|
133 | + public function markFieldDirty($name) { |
|
134 | 134 | $this->dirty->$name = true; // field became dirty |
135 | 135 | } |
136 | 136 | |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | * @param string $name |
141 | 141 | * @return bool |
142 | 142 | */ |
143 | - public function isFieldDirty( $name ) { |
|
143 | + public function isFieldDirty($name) { |
|
144 | 144 | return isset($this->dirty->$name) && ($this->dirty->$name == true); |
145 | 145 | } |
146 | 146 | |
@@ -160,20 +160,20 @@ discard block |
||
160 | 160 | * @return mixed |
161 | 161 | * @throws \Exception |
162 | 162 | */ |
163 | - public function __get( $name ) { |
|
163 | + public function __get($name) { |
|
164 | 164 | if (!$this->hasData()) { |
165 | - throw new \Exception( "data property=$name has not been initialised", 1 ); |
|
165 | + throw new \Exception("data property=$name has not been initialised", 1); |
|
166 | 166 | } |
167 | 167 | |
168 | - if (property_exists( $this->data, $name )) { |
|
168 | + if (property_exists($this->data, $name)) { |
|
169 | 169 | return $this->data->$name; |
170 | 170 | } |
171 | 171 | |
172 | 172 | $trace = debug_backtrace(); |
173 | 173 | throw new \Exception( |
174 | - 'Undefined property via __get(): ' . $name . |
|
175 | - ' in ' . $trace[0]['file'] . |
|
176 | - ' on line ' . $trace[0]['line'], |
|
174 | + 'Undefined property via __get(): '.$name. |
|
175 | + ' in '.$trace[0]['file']. |
|
176 | + ' on line '.$trace[0]['line'], |
|
177 | 177 | 1 |
178 | 178 | ); |
179 | 179 | } |
@@ -186,8 +186,8 @@ discard block |
||
186 | 186 | * |
187 | 187 | * @return bool |
188 | 188 | */ |
189 | - public function __isset( $name ) { |
|
190 | - if ($this->hasData() && property_exists( $this->data, $name )) { |
|
189 | + public function __isset($name) { |
|
190 | + if ($this->hasData() && property_exists($this->data, $name)) { |
|
191 | 191 | return true; |
192 | 192 | } |
193 | 193 | |
@@ -205,9 +205,9 @@ discard block |
||
205 | 205 | * @param string $password |
206 | 206 | * @param array $driverOptions |
207 | 207 | */ |
208 | - public static function connectDb( $dsn, $username, $password, $driverOptions = array() ) { |
|
209 | - static::$_db = new \PDO( $dsn, $username, $password, $driverOptions ); |
|
210 | - static::$_db->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION ); // Set Errorhandling to Exception |
|
208 | + public static function connectDb($dsn, $username, $password, $driverOptions = array()) { |
|
209 | + static::$_db = new \PDO($dsn, $username, $password, $driverOptions); |
|
210 | + static::$_db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // Set Errorhandling to Exception |
|
211 | 211 | static::_setup_identifier_quote_character(); |
212 | 212 | } |
213 | 213 | |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | * @return void |
219 | 219 | */ |
220 | 220 | public static function _setup_identifier_quote_character() { |
221 | - if (is_null( static::$_identifier_quote_character )) { |
|
221 | + if (is_null(static::$_identifier_quote_character)) { |
|
222 | 222 | static::$_identifier_quote_character = static::_detect_identifier_quote_character(); |
223 | 223 | } |
224 | 224 | } |
@@ -252,9 +252,9 @@ discard block |
||
252 | 252 | */ |
253 | 253 | protected static function getDriverName() { |
254 | 254 | if (!static::$_db) { |
255 | - throw new \Exception( 'No database connection setup' ); |
|
255 | + throw new \Exception('No database connection setup'); |
|
256 | 256 | } |
257 | - return static::$_db->getAttribute( \PDO::ATTR_DRIVER_NAME ); |
|
257 | + return static::$_db->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | /** |
@@ -265,14 +265,14 @@ discard block |
||
265 | 265 | * @param string $identifier |
266 | 266 | * @return string |
267 | 267 | */ |
268 | - protected static function _quote_identifier( $identifier ) { |
|
268 | + protected static function _quote_identifier($identifier) { |
|
269 | 269 | $class = get_called_class(); |
270 | - $parts = explode( '.', $identifier ); |
|
271 | - $parts = array_map( array( |
|
270 | + $parts = explode('.', $identifier); |
|
271 | + $parts = array_map(array( |
|
272 | 272 | $class, |
273 | 273 | '_quote_identifier_part' |
274 | - ), $parts ); |
|
275 | - return join( '.', $parts ); |
|
274 | + ), $parts); |
|
275 | + return join('.', $parts); |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | |
@@ -284,11 +284,11 @@ discard block |
||
284 | 284 | * @param string $part |
285 | 285 | * @return string |
286 | 286 | */ |
287 | - protected static function _quote_identifier_part( $part ) { |
|
287 | + protected static function _quote_identifier_part($part) { |
|
288 | 288 | if ($part === '*') { |
289 | 289 | return $part; |
290 | 290 | } |
291 | - return static::$_identifier_quote_character . $part . static::$_identifier_quote_character; |
|
291 | + return static::$_identifier_quote_character.$part.static::$_identifier_quote_character; |
|
292 | 292 | } |
293 | 293 | |
294 | 294 | /** |
@@ -299,8 +299,8 @@ discard block |
||
299 | 299 | protected static function getFieldnames() { |
300 | 300 | $class = get_called_class(); |
301 | 301 | if (!isset(self::$_tableColumns[$class])) { |
302 | - $st = static::execute( 'DESCRIBE ' . static::_quote_identifier( static::$_tableName ) ); |
|
303 | - self::$_tableColumns[$class] = $st->fetchAll( \PDO::FETCH_COLUMN ); |
|
302 | + $st = static::execute('DESCRIBE '.static::_quote_identifier(static::$_tableName)); |
|
303 | + self::$_tableColumns[$class] = $st->fetchAll(\PDO::FETCH_COLUMN); |
|
304 | 304 | } |
305 | 305 | return self::$_tableColumns[$class]; |
306 | 306 | } |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | * |
313 | 313 | * @return void |
314 | 314 | */ |
315 | - public function hydrate( $data ) { |
|
315 | + public function hydrate($data) { |
|
316 | 316 | foreach (static::getFieldnames() as $fieldname) { |
317 | 317 | if (isset($data[$fieldname])) { |
318 | 318 | $this->$fieldname = $data[$fieldname]; |
@@ -358,8 +358,8 @@ discard block |
||
358 | 358 | * @param string $id |
359 | 359 | * @return Object |
360 | 360 | */ |
361 | - static public function getById( $id ) { |
|
362 | - return static::fetchOneWhere( static::_quote_identifier( static::$_primary_column_name ) . ' = ?', array($id) ); |
|
361 | + static public function getById($id) { |
|
362 | + return static::fetchOneWhere(static::_quote_identifier(static::$_primary_column_name).' = ?', array($id)); |
|
363 | 363 | } |
364 | 364 | |
365 | 365 | /** |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | * @return Object |
369 | 369 | */ |
370 | 370 | static public function first() { |
371 | - return static::fetchOneWhere( '1=1 ORDER BY ' . static::_quote_identifier( static::$_primary_column_name ) . ' ASC' ); |
|
371 | + return static::fetchOneWhere('1=1 ORDER BY '.static::_quote_identifier(static::$_primary_column_name).' ASC'); |
|
372 | 372 | } |
373 | 373 | |
374 | 374 | /** |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | * @return Object |
378 | 378 | */ |
379 | 379 | static public function last() { |
380 | - return static::fetchOneWhere( '1=1 ORDER BY ' . static::_quote_identifier( static::$_primary_column_name ) . ' DESC' ); |
|
380 | + return static::fetchOneWhere('1=1 ORDER BY '.static::_quote_identifier(static::$_primary_column_name).' DESC'); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | /** |
@@ -386,9 +386,9 @@ discard block |
||
386 | 386 | * @param string $id |
387 | 387 | * @return object[] of objects for matching records |
388 | 388 | */ |
389 | - static public function find( $id ) { |
|
390 | - $find_by_method = 'find_by_' . (static::$_primary_column_name); |
|
391 | - static::$find_by_method( $id ); |
|
389 | + static public function find($id) { |
|
390 | + $find_by_method = 'find_by_'.(static::$_primary_column_name); |
|
391 | + static::$find_by_method($id); |
|
392 | 392 | } |
393 | 393 | |
394 | 394 | /** |
@@ -403,39 +403,39 @@ discard block |
||
403 | 403 | * @return mixed int|object[]|object |
404 | 404 | * @throws \Exception |
405 | 405 | */ |
406 | - static public function __callStatic( $name, $arguments ) { |
|
406 | + static public function __callStatic($name, $arguments) { |
|
407 | 407 | // Note: value of $name is case sensitive. |
408 | - if (preg_match( '/^find_by_/', $name ) == 1) { |
|
408 | + if (preg_match('/^find_by_/', $name) == 1) { |
|
409 | 409 | // it's a find_by_{fieldname} dynamic method |
410 | - $fieldname = substr( $name, 8 ); // remove find by |
|
410 | + $fieldname = substr($name, 8); // remove find by |
|
411 | 411 | $match = $arguments[0]; |
412 | - return static::fetchAllWhereMatchingSingleField( $fieldname, $match ); |
|
413 | - } else if (preg_match( '/^findOne_by_/', $name ) == 1) { |
|
412 | + return static::fetchAllWhereMatchingSingleField($fieldname, $match); |
|
413 | + } else if (preg_match('/^findOne_by_/', $name) == 1) { |
|
414 | 414 | // it's a findOne_by_{fieldname} dynamic method |
415 | - $fieldname = substr( $name, 11 ); // remove findOne_by_ |
|
415 | + $fieldname = substr($name, 11); // remove findOne_by_ |
|
416 | 416 | $match = $arguments[0]; |
417 | - return static::fetchOneWhereMatchingSingleField( $fieldname, $match, 'ASC' ); |
|
418 | - } else if (preg_match( '/^first_by_/', $name ) == 1) { |
|
417 | + return static::fetchOneWhereMatchingSingleField($fieldname, $match, 'ASC'); |
|
418 | + } else if (preg_match('/^first_by_/', $name) == 1) { |
|
419 | 419 | // it's a first_by_{fieldname} dynamic method |
420 | - $fieldname = substr( $name, 9 ); // remove first_by_ |
|
420 | + $fieldname = substr($name, 9); // remove first_by_ |
|
421 | 421 | $match = $arguments[0]; |
422 | - return static::fetchOneWhereMatchingSingleField( $fieldname, $match, 'ASC' ); |
|
423 | - } else if (preg_match( '/^last_by_/', $name ) == 1) { |
|
422 | + return static::fetchOneWhereMatchingSingleField($fieldname, $match, 'ASC'); |
|
423 | + } else if (preg_match('/^last_by_/', $name) == 1) { |
|
424 | 424 | // it's a last_by_{fieldname} dynamic method |
425 | - $fieldname = substr( $name, 8 ); // remove last_by_ |
|
425 | + $fieldname = substr($name, 8); // remove last_by_ |
|
426 | 426 | $match = $arguments[0]; |
427 | - return static::fetchOneWhereMatchingSingleField( $fieldname, $match, 'DESC' ); |
|
428 | - } else if (preg_match( '/^count_by_/', $name ) == 1) { |
|
427 | + return static::fetchOneWhereMatchingSingleField($fieldname, $match, 'DESC'); |
|
428 | + } else if (preg_match('/^count_by_/', $name) == 1) { |
|
429 | 429 | // it's a count_by_{fieldname} dynamic method |
430 | - $fieldname = substr( $name, 9 ); // remove find by |
|
430 | + $fieldname = substr($name, 9); // remove find by |
|
431 | 431 | $match = $arguments[0]; |
432 | - if (is_array( $match )) { |
|
433 | - return static::countAllWhere( static::_quote_identifier( $fieldname ) . ' IN (' . static::createInClausePlaceholders( $match ) . ')', $match ); |
|
432 | + if (is_array($match)) { |
|
433 | + return static::countAllWhere(static::_quote_identifier($fieldname).' IN ('.static::createInClausePlaceholders($match).')', $match); |
|
434 | 434 | } else { |
435 | - return static::countAllWhere( static::_quote_identifier( $fieldname ) . ' = ?', array($match) ); |
|
435 | + return static::countAllWhere(static::_quote_identifier($fieldname).' = ?', array($match)); |
|
436 | 436 | } |
437 | 437 | } |
438 | - throw new \Exception( __CLASS__ . ' not such static method[' . $name . ']' ); |
|
438 | + throw new \Exception(__CLASS__.' not such static method['.$name.']'); |
|
439 | 439 | } |
440 | 440 | |
441 | 441 | /** |
@@ -446,11 +446,11 @@ discard block |
||
446 | 446 | * @param string $order ASC|DESC |
447 | 447 | * @return object of calling class |
448 | 448 | */ |
449 | - public static function fetchOneWhereMatchingSingleField( $fieldname, $match, $order ) { |
|
450 | - if (is_array( $match )) { |
|
451 | - return static::fetchOneWhere( static::_quote_identifier( $fieldname ) . ' IN (' . static::createInClausePlaceholders( $match ) . ') ORDER BY ' . static::_quote_identifier( $fieldname ) . ' ' . $order, $match ); |
|
449 | + public static function fetchOneWhereMatchingSingleField($fieldname, $match, $order) { |
|
450 | + if (is_array($match)) { |
|
451 | + return static::fetchOneWhere(static::_quote_identifier($fieldname).' IN ('.static::createInClausePlaceholders($match).') ORDER BY '.static::_quote_identifier($fieldname).' '.$order, $match); |
|
452 | 452 | } else { |
453 | - return static::fetchOneWhere( static::_quote_identifier( $fieldname ) . ' = ? ORDER BY ' . static::_quote_identifier( $fieldname ) . ' ' . $order, array($match) ); |
|
453 | + return static::fetchOneWhere(static::_quote_identifier($fieldname).' = ? ORDER BY '.static::_quote_identifier($fieldname).' '.$order, array($match)); |
|
454 | 454 | } |
455 | 455 | } |
456 | 456 | |
@@ -462,11 +462,11 @@ discard block |
||
462 | 462 | * @param string|array $match |
463 | 463 | * @return object[] of objects of calling class |
464 | 464 | */ |
465 | - public static function fetchAllWhereMatchingSingleField( $fieldname, $match ) { |
|
466 | - if (is_array( $match )) { |
|
467 | - return static::fetchAllWhere( static::_quote_identifier( $fieldname ) . ' IN (' . static::createInClausePlaceholders( $match ) . ')', $match ); |
|
465 | + public static function fetchAllWhereMatchingSingleField($fieldname, $match) { |
|
466 | + if (is_array($match)) { |
|
467 | + return static::fetchAllWhere(static::_quote_identifier($fieldname).' IN ('.static::createInClausePlaceholders($match).')', $match); |
|
468 | 468 | } else { |
469 | - return static::fetchAllWhere( static::_quote_identifier( $fieldname ) . ' = ?', array($match) ); |
|
469 | + return static::fetchAllWhere(static::_quote_identifier($fieldname).' = ?', array($match)); |
|
470 | 470 | } |
471 | 471 | } |
472 | 472 | |
@@ -476,8 +476,8 @@ discard block |
||
476 | 476 | * @param array $params |
477 | 477 | * @return string |
478 | 478 | */ |
479 | - static public function createInClausePlaceholders( $params ) { |
|
480 | - return implode( ',', array_fill( 0, count( $params ), '?' ) ); |
|
479 | + static public function createInClausePlaceholders($params) { |
|
480 | + return implode(',', array_fill(0, count($params), '?')); |
|
481 | 481 | } |
482 | 482 | |
483 | 483 | /** |
@@ -486,8 +486,8 @@ discard block |
||
486 | 486 | * @return int |
487 | 487 | */ |
488 | 488 | static public function count() { |
489 | - $st = static::execute( 'SELECT COUNT(*) FROM ' . static::_quote_identifier( static::$_tableName ) ); |
|
490 | - return (int)$st->fetchColumn( 0 ); |
|
489 | + $st = static::execute('SELECT COUNT(*) FROM '.static::_quote_identifier(static::$_tableName)); |
|
490 | + return (int) $st->fetchColumn(0); |
|
491 | 491 | } |
492 | 492 | |
493 | 493 | /** |
@@ -497,10 +497,10 @@ discard block |
||
497 | 497 | * @param array $params optional params to be escaped and injected into the SQL query (standrd PDO syntax) |
498 | 498 | * @return integer count of rows matching conditions |
499 | 499 | */ |
500 | - static public function countAllWhere( $SQLfragment = '', $params = array() ) { |
|
501 | - $SQLfragment = self::addWherePrefix( $SQLfragment ); |
|
502 | - $st = static::execute( 'SELECT COUNT(*) FROM ' . static::_quote_identifier( static::$_tableName ) . $SQLfragment, $params ); |
|
503 | - return (int)$st->fetchColumn( 0 ); |
|
500 | + static public function countAllWhere($SQLfragment = '', $params = array()) { |
|
501 | + $SQLfragment = self::addWherePrefix($SQLfragment); |
|
502 | + $st = static::execute('SELECT COUNT(*) FROM '.static::_quote_identifier(static::$_tableName).$SQLfragment, $params); |
|
503 | + return (int) $st->fetchColumn(0); |
|
504 | 504 | } |
505 | 505 | |
506 | 506 | /** |
@@ -509,8 +509,8 @@ discard block |
||
509 | 509 | * @param string $SQLfragment |
510 | 510 | * @return string |
511 | 511 | */ |
512 | - static protected function addWherePrefix( $SQLfragment ) { |
|
513 | - return $SQLfragment ? ' WHERE ' . $SQLfragment : $SQLfragment; |
|
512 | + static protected function addWherePrefix($SQLfragment) { |
|
513 | + return $SQLfragment ? ' WHERE '.$SQLfragment : $SQLfragment; |
|
514 | 514 | } |
515 | 515 | |
516 | 516 | |
@@ -522,20 +522,20 @@ discard block |
||
522 | 522 | * @param bool $limitOne if true the first match will be returned |
523 | 523 | * @return mixed object[]|object of objects of calling class |
524 | 524 | */ |
525 | - static public function fetchWhere( $SQLfragment = '', $params = array(), $limitOne = false ) { |
|
525 | + static public function fetchWhere($SQLfragment = '', $params = array(), $limitOne = false) { |
|
526 | 526 | $class = get_called_class(); |
527 | - $SQLfragment = self::addWherePrefix( $SQLfragment ); |
|
527 | + $SQLfragment = self::addWherePrefix($SQLfragment); |
|
528 | 528 | $st = static::execute( |
529 | - 'SELECT * FROM ' . static::_quote_identifier( static::$_tableName ) . $SQLfragment . ($limitOne ? ' LIMIT 1' : ''), |
|
529 | + 'SELECT * FROM '.static::_quote_identifier(static::$_tableName).$SQLfragment.($limitOne ? ' LIMIT 1' : ''), |
|
530 | 530 | $params |
531 | 531 | ); |
532 | - $st->setFetchMode( \PDO::FETCH_ASSOC ); |
|
532 | + $st->setFetchMode(\PDO::FETCH_ASSOC); |
|
533 | 533 | if ($limitOne) { |
534 | - return new $class( $st->fetch() ); |
|
534 | + return new $class($st->fetch()); |
|
535 | 535 | } |
536 | 536 | $results = []; |
537 | 537 | foreach ($st->fetch() as $row) { |
538 | - $results[] = new $class( $row ); |
|
538 | + $results[] = new $class($row); |
|
539 | 539 | } |
540 | 540 | return $results; |
541 | 541 | } |
@@ -547,8 +547,8 @@ discard block |
||
547 | 547 | * @param array $params optional params to be escaped and injected into the SQL query (standrd PDO syntax) |
548 | 548 | * @return object[] of objects of calling class |
549 | 549 | */ |
550 | - static public function fetchAllWhere( $SQLfragment = '', $params = array() ) { |
|
551 | - return static::fetchWhere( $SQLfragment, $params, false ); |
|
550 | + static public function fetchAllWhere($SQLfragment = '', $params = array()) { |
|
551 | + return static::fetchWhere($SQLfragment, $params, false); |
|
552 | 552 | } |
553 | 553 | |
554 | 554 | /** |
@@ -558,8 +558,8 @@ discard block |
||
558 | 558 | * @param array $params optional params to be escaped and injected into the SQL query (standrd PDO syntax) |
559 | 559 | * @return object of calling class |
560 | 560 | */ |
561 | - static public function fetchOneWhere( $SQLfragment = '', $params = array() ) { |
|
562 | - return static::fetchWhere( $SQLfragment, $params, true ); |
|
561 | + static public function fetchOneWhere($SQLfragment = '', $params = array()) { |
|
562 | + return static::fetchWhere($SQLfragment, $params, true); |
|
563 | 563 | } |
564 | 564 | |
565 | 565 | /** |
@@ -567,9 +567,9 @@ discard block |
||
567 | 567 | * |
568 | 568 | * @return boolean indicating success |
569 | 569 | */ |
570 | - static public function deleteById( $id ) { |
|
570 | + static public function deleteById($id) { |
|
571 | 571 | $st = static::execute( |
572 | - 'DELETE FROM ' . static::_quote_identifier( static::$_tableName ) . ' WHERE ' . static::_quote_identifier( static::$_primary_column_name ) . ' = ? LIMIT 1', |
|
572 | + 'DELETE FROM '.static::_quote_identifier(static::$_tableName).' WHERE '.static::_quote_identifier(static::$_primary_column_name).' = ? LIMIT 1', |
|
573 | 573 | array($id) |
574 | 574 | ); |
575 | 575 | return ($st->rowCount() == 1); |
@@ -581,7 +581,7 @@ discard block |
||
581 | 581 | * @return boolean indicating success |
582 | 582 | */ |
583 | 583 | public function delete() { |
584 | - return self::deleteById( $this->{static::$_primary_column_name} ); |
|
584 | + return self::deleteById($this->{static::$_primary_column_name} ); |
|
585 | 585 | } |
586 | 586 | |
587 | 587 | /** |
@@ -591,9 +591,9 @@ discard block |
||
591 | 591 | * @param array $params optional params to be escaped and injected into the SQL query (standrd PDO syntax) |
592 | 592 | * @return \PDOStatement |
593 | 593 | */ |
594 | - static public function deleteAllWhere( $where, $params = array() ) { |
|
594 | + static public function deleteAllWhere($where, $params = array()) { |
|
595 | 595 | $st = static::execute( |
596 | - 'DELETE FROM ' . static::_quote_identifier( static::$_tableName ) . ' WHERE ' . $where, |
|
596 | + 'DELETE FROM '.static::_quote_identifier(static::$_tableName).' WHERE '.$where, |
|
597 | 597 | $params |
598 | 598 | ); |
599 | 599 | return $st; |
@@ -616,22 +616,22 @@ discard block |
||
616 | 616 | * @param boolean $allowSetPrimaryKey if true include primary key field in insert (ie. you want to set it yourself) |
617 | 617 | * @return boolean indicating success |
618 | 618 | */ |
619 | - public function insert( $autoTimestamp = true, $allowSetPrimaryKey = false ) { |
|
619 | + public function insert($autoTimestamp = true, $allowSetPrimaryKey = false) { |
|
620 | 620 | $pk = static::$_primary_column_name; |
621 | - $timeStr = gmdate( 'Y-m-d H:i:s' ); |
|
622 | - if ($autoTimestamp && in_array( 'created_at', static::getFieldnames() )) { |
|
621 | + $timeStr = gmdate('Y-m-d H:i:s'); |
|
622 | + if ($autoTimestamp && in_array('created_at', static::getFieldnames())) { |
|
623 | 623 | $this->created_at = $timeStr; |
624 | 624 | } |
625 | - if ($autoTimestamp && in_array( 'updated_at', static::getFieldnames() )) { |
|
625 | + if ($autoTimestamp && in_array('updated_at', static::getFieldnames())) { |
|
626 | 626 | $this->updated_at = $timeStr; |
627 | 627 | } |
628 | 628 | $this->validate(); |
629 | 629 | if ($allowSetPrimaryKey !== true) { |
630 | 630 | $this->$pk = null; // ensure id is null |
631 | 631 | } |
632 | - $set = $this->setString( !$allowSetPrimaryKey ); |
|
633 | - $query = 'INSERT INTO ' . static::_quote_identifier( static::$_tableName ) . ' SET ' . $set['sql']; |
|
634 | - $st = static::execute( $query, $set['params'] ); |
|
632 | + $set = $this->setString(!$allowSetPrimaryKey); |
|
633 | + $query = 'INSERT INTO '.static::_quote_identifier(static::$_tableName).' SET '.$set['sql']; |
|
634 | + $st = static::execute($query, $set['params']); |
|
635 | 635 | if ($st->rowCount() == 1) { |
636 | 636 | $this->{static::$_primary_column_name} = static::$_db->lastInsertId(); |
637 | 637 | $this->clearDirtyFields(); |
@@ -645,13 +645,13 @@ discard block |
||
645 | 645 | * @param boolean $autoTimestamp true by default will set updated_at field if present |
646 | 646 | * @return boolean indicating success |
647 | 647 | */ |
648 | - public function update( $autoTimestamp = true ) { |
|
649 | - if ($autoTimestamp && in_array( 'updated_at', static::getFieldnames() )) { |
|
650 | - $this->updated_at = gmdate( 'Y-m-d H:i:s' ); |
|
648 | + public function update($autoTimestamp = true) { |
|
649 | + if ($autoTimestamp && in_array('updated_at', static::getFieldnames())) { |
|
650 | + $this->updated_at = gmdate('Y-m-d H:i:s'); |
|
651 | 651 | } |
652 | 652 | $this->validate(); |
653 | 653 | $set = $this->setString(); |
654 | - $query = 'UPDATE ' . static::_quote_identifier( static::$_tableName ) . ' SET ' . $set['sql'] . ' WHERE ' . static::_quote_identifier( static::$_primary_column_name ) . ' = ? LIMIT 1'; |
|
654 | + $query = 'UPDATE '.static::_quote_identifier(static::$_tableName).' SET '.$set['sql'].' WHERE '.static::_quote_identifier(static::$_primary_column_name).' = ? LIMIT 1'; |
|
655 | 655 | $set['params'][] = $this->{static::$_primary_column_name}; |
656 | 656 | $st = static::execute( |
657 | 657 | $query, |
@@ -672,9 +672,9 @@ discard block |
||
672 | 672 | * @param array $params array of parameters to replace the placeholders in the statement |
673 | 673 | * @return \PDOStatement handle |
674 | 674 | */ |
675 | - public static function execute( $query, $params = array() ) { |
|
676 | - $st = static::_prepare( $query ); |
|
677 | - $st->execute( $params ); |
|
675 | + public static function execute($query, $params = array()) { |
|
676 | + $st = static::_prepare($query); |
|
677 | + $st->execute($params); |
|
678 | 678 | return $st; |
679 | 679 | } |
680 | 680 | |
@@ -684,10 +684,10 @@ discard block |
||
684 | 684 | * @param string $query |
685 | 685 | * @return \PDOStatement |
686 | 686 | */ |
687 | - protected static function _prepare( $query ) { |
|
687 | + protected static function _prepare($query) { |
|
688 | 688 | if (!isset(static::$_stmt[$query])) { |
689 | 689 | // cache prepared query if not seen before |
690 | - static::$_stmt[$query] = static::$_db->prepare( $query ); |
|
690 | + static::$_stmt[$query] = static::$_db->prepare($query); |
|
691 | 691 | } |
692 | 692 | return static::$_stmt[$query]; // return cache copy |
693 | 693 | } |
@@ -713,7 +713,7 @@ discard block |
||
713 | 713 | * @param boolean $ignorePrimary |
714 | 714 | * @return string[string] ['sql' => string, 'params' => mixed[] ] |
715 | 715 | */ |
716 | - protected function setString( $ignorePrimary = true ) { |
|
716 | + protected function setString($ignorePrimary = true) { |
|
717 | 717 | // escapes and builds mysql SET string returning false, empty string or `field` = 'val'[, `field` = 'val']... |
718 | 718 | /** |
719 | 719 | * @var array $fragments individual SQL assignments |
@@ -727,18 +727,18 @@ discard block |
||
727 | 727 | if ($ignorePrimary && $field == static::$_primary_column_name) { |
728 | 728 | continue; |
729 | 729 | } |
730 | - if (isset($this->$field) && $this->isFieldDirty( $field )) { // Only if dirty |
|
730 | + if (isset($this->$field) && $this->isFieldDirty($field)) { // Only if dirty |
|
731 | 731 | if ($this->$field === null) { |
732 | 732 | // if empty set to NULL |
733 | - $fragments[] = static::_quote_identifier( $field ) . ' = NULL'; |
|
733 | + $fragments[] = static::_quote_identifier($field).' = NULL'; |
|
734 | 734 | } else { |
735 | 735 | // Just set value normally as not empty string with NULL allowed |
736 | - $fragments[] = static::_quote_identifier( $field ) . ' = ?'; |
|
736 | + $fragments[] = static::_quote_identifier($field).' = ?'; |
|
737 | 737 | $params[] = $this->$field; |
738 | 738 | } |
739 | 739 | } |
740 | 740 | } |
741 | - $sqlFragment = implode( ", ", $fragments ); |
|
741 | + $sqlFragment = implode(", ", $fragments); |
|
742 | 742 | return [ |
743 | 743 | 'sql' => $sqlFragment, |
744 | 744 | 'params' => $params |
@@ -751,9 +751,9 @@ discard block |
||
751 | 751 | * @param string|int $dt a date string or a unix timestamp |
752 | 752 | * @return string |
753 | 753 | */ |
754 | - public static function datetimeToMysqldatetime( $dt ) { |
|
755 | - $dt = (is_string( $dt )) ? strtotime( $dt ) : $dt; |
|
756 | - return date( 'Y-m-d H:i:s', $dt ); |
|
754 | + public static function datetimeToMysqldatetime($dt) { |
|
755 | + $dt = (is_string($dt)) ? strtotime($dt) : $dt; |
|
756 | + return date('Y-m-d H:i:s', $dt); |
|
757 | 757 | } |
758 | 758 | } |
759 | 759 |