@@ -390,7 +390,14 @@ public function insert($data, $skipValidation = null)
390
390
if ($ data !== false ) {
391
391
$ data = $ this ->trigger ('beforeInsert ' , array ('method ' => 'insert ' , 'fields ' => $ data ));
392
392
393
- $ result = $ this ->db ->insert ($ this ->table (), $ this ->prepareData ($ data ));
393
+ // Prepare the Data
394
+ $ data = $ this ->prepareData ($ data );
395
+
396
+ // Get the parameter Types.
397
+ $ paramTypes = self ::getParamTypes ($ data );
398
+
399
+ // Execute the INSERT.
400
+ $ result = $ this ->db ->insert ($ this ->table (), $ data , $ paramTypes );
394
401
395
402
if ($ result !== false ) {
396
403
$ this ->trigger ('afterInsert ' , array ('id ' => $ result , 'fields ' => $ data , 'method ' => 'insert ' ));
@@ -423,9 +430,11 @@ public function replace($data, $skipValidation = null)
423
430
424
431
// Will be false if it didn't validate.
425
432
if ($ data !== false ) {
433
+ $ data = $ this ->prepareData ($ data );
434
+
426
435
$ paramTypes = self ::getParamTypes ($ data );
427
436
428
- $ result = $ this ->db ->replace ($ this ->table (), $ this -> prepareData ( $ data) , $ paramTypes );
437
+ $ result = $ this ->db ->replace ($ this ->table (), $ data , $ paramTypes );
429
438
430
439
if ($ result !== false ) {
431
440
$ this ->trigger ('afterInsert ' , array ('id ' => $ id , 'fields ' => $ data , 'method ' => 'replace ' ));
@@ -460,7 +469,16 @@ public function update($id, $data, $skipValidation = null)
460
469
if ($ data !== false ) {
461
470
$ data = $ this ->trigger ('beforeUpdate ' , array ('id ' => $ id , 'method ' =>'update ' , 'fields ' => $ data ));
462
471
463
- $ result = $ this ->db ->update ($ this ->table (), $ this ->prepareData ($ data ), array ($ this ->primaryKey => $ id ));
472
+ // Prepare the Data.
473
+ $ data = $ this ->prepareData ($ data );
474
+
475
+ // Calculate the parameter Types.
476
+ $ paramTypes = self ::getParamTypes ($ data );
477
+
478
+ $ paramTypes [$ this ->primaryKey ] = is_integer ($ id ) ? PDO ::PARAM_INT : PDO ::PARAM_STR ;
479
+
480
+ // Execute the UPDATE.
481
+ $ result = $ this ->db ->update ($ this ->table (), $ data , array ($ this ->primaryKey => $ id ), $ paramTypes );
464
482
465
483
$ result = $ this ->trigger ('afterUpdate ' , array ('id ' => $ id , 'method ' => 'update ' , 'fields ' => $ data , 'result ' => $ result ));
466
484
}
@@ -513,29 +531,24 @@ public function updateMany($ids, $data, $skipValidation = null)
513
531
//
514
532
$ this ->db ->connect ();
515
533
516
- // Prepare the SET command and parameter types .
517
- $ set = array ();
534
+ // Prepare the SET command and parameter Types .
535
+ $ setFields = array ();
518
536
519
537
$ paramTypes = array ();
520
538
521
- foreach ($ data as $ columnName => $ value ) {
522
- $ set [] = $ columnName .' = : ' .$ columnName ;
539
+ foreach ($ data as $ field => $ value ) {
540
+ $ setFields [] = $ field .' = : ' .$ field ;
523
541
524
- if (is_integer ($ value )) {
525
- $ paramTypes [$ columnName ] = PDO ::PARAM_INT ;
526
- }
527
- else {
528
- $ paramTypes [$ columnName ] = PDO ::PARAM_STR ;
529
- }
542
+ $ paramTypes [$ field ] = is_integer ($ value ) ? PDO ::PARAM_INT : PDO ::PARAM_STR ;
530
543
}
531
544
532
545
// Prepare the parameters.
533
- $ params = array_merge ( $ data , array ( ' ids ' => $ ids)) ;
546
+ $ params[ ' values ' ] = $ ids ;
534
547
535
- $ paramTypes ['ids ' ] = Connection::PARAM_INT_ARRAY ;
548
+ $ paramTypes ['values ' ] = Connection::PARAM_INT_ARRAY ;
536
549
537
550
// Prepare the SQL Statement.
538
- $ sql = "UPDATE " .$ this ->table () ." SET " . implode (', ' , $ set ) ." WHERE " .$ this ->primaryKey ." IN (:ids ) " ;
551
+ $ sql = "UPDATE " .$ this ->table () ." SET " . implode (', ' , $ setFields ) ." WHERE " .$ this ->primaryKey ." IN (:values ) " ;
539
552
540
553
$ result = $ this ->db ->executeUpdate ($ sql , $ params , $ paramTypes );
541
554
@@ -584,7 +597,14 @@ public function updateBy()
584
597
585
598
// Will be false if it didn't validate.
586
599
if (($ data = $ this ->validate ($ data )) !== false ) {
587
- $ result = $ this ->db ->update ($ this ->table (), $ this ->prepareData ($ data ), $ this ->wheres ());
600
+ // Prepare the Data.
601
+ $ data = $ this ->prepareData ($ data );
602
+
603
+ // Calculate the parameter Types.
604
+ $ paramTypes = self ::getParamTypes ($ data );
605
+
606
+ // Execute the UPDATE.
607
+ $ result = $ this ->db ->update ($ this ->table (), $ data , $ this ->wheres (), $ paramTypes );
588
608
589
609
$ this ->trigger ('afterUpdate ' , array ('method ' => 'updateBy ' , 'fields ' => $ data , 'result ' => $ result ));
590
610
}
@@ -616,7 +636,14 @@ public function updateAll($data, $skipValidation = null)
616
636
617
637
// Will be false if it didn't validate.
618
638
if ($ data !== false ) {
619
- $ result = $ this ->db ->update ($ this ->table (), $ this ->prepareData ($ data ), true );
639
+ // Prepare the Data.
640
+ $ data = $ this ->prepareData ($ data );
641
+
642
+ // Calculate the parameter Types.
643
+ $ paramTypes = self ::getParamTypes ($ data );
644
+
645
+ // Execute the UPDATE.
646
+ $ result = $ this ->db ->update ($ this ->table (), $ data , array (1 => 1 ), $ paramTypes );
620
647
621
648
$ this ->trigger ('afterUpdate ' , array ('method ' => 'updateAll ' , 'fields ' => $ data , 'result ' => $ result ));
622
649
}
@@ -1461,14 +1488,20 @@ protected function parseSelectOrder()
1461
1488
return $ result ;
1462
1489
}
1463
1490
1464
- protected static function getParamTypes ($ params )
1491
+ protected static function getParamTypes (array $ params )
1465
1492
{
1466
- $ result[] = array ();
1493
+ $ result = array ();
1467
1494
1468
1495
foreach ($ params as $ key => $ value ) {
1469
1496
if (is_integer ($ value )) {
1470
1497
$ result [$ key ] = PDO ::PARAM_INT ;
1471
1498
}
1499
+ else if (is_bool ($ value )) {
1500
+ $ result [$ key ] = PDO ::PARAM_BOOL ;
1501
+ }
1502
+ else if ($ value === null ) {
1503
+ $ result [$ key ] = PDO ::PARAM_NULL ;
1504
+ }
1472
1505
else {
1473
1506
$ result [$ key ] = PDO ::PARAM_STR ;
1474
1507
}
0 commit comments