7
7
* @author Alexander V. Butenko <[email protected] >
8
8
* @copyright Copyright (c) 2015-2017
9
9
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
10
- * @link http://github.com/joshcam/PHP-MySQLi-Database-Class
10
+ * @link http://github.com/joshcam/PHP-MySQLi-Database-Class
11
11
* @version 2.9-master
12
12
*
13
13
* @method int count ()
@@ -106,6 +106,11 @@ class dbObject {
106
106
*/
107
107
protected $ dbTable ;
108
108
109
+ /**
110
+ * @var array name of the fields that will be skipped during validation, preparing & saving
111
+ */
112
+ protected $ toSkip = array ();
113
+
109
114
/**
110
115
* @param array $data Data to preload on object creation
111
116
*/
@@ -241,7 +246,7 @@ public function insert () {
241
246
if (!empty ($ this ->primaryKey ) && empty ($ this ->data [$ this ->primaryKey ]))
242
247
$ this ->data [$ this ->primaryKey ] = $ id ;
243
248
$ this ->isNew = false ;
244
-
249
+ $ this -> toSkip = array ();
245
250
return $ id ;
246
251
}
247
252
@@ -256,8 +261,12 @@ public function update ($data = null) {
256
261
return false ;
257
262
258
263
if ($ data ) {
259
- foreach ($ data as $ k => $ v )
260
- $ this ->$ k = $ v ;
264
+ foreach ($ data as $ k => $ v ) {
265
+ if (in_array ($ k , $ this ->toSkip ))
266
+ continue ;
267
+
268
+ $ this ->$ k = $ v ;
269
+ }
261
270
}
262
271
263
272
if (!empty ($ this ->timestamps ) && in_array ("updatedAt " , $ this ->timestamps ))
@@ -268,7 +277,9 @@ public function update ($data = null) {
268
277
return false ;
269
278
270
279
$ this ->db ->where ($ this ->primaryKey , $ this ->data [$ this ->primaryKey ]);
271
- return $ this ->db ->update ($ this ->dbTable , $ sqlData );
280
+ $ res = $ this ->db ->update ($ this ->dbTable , $ sqlData );
281
+ $ this ->toSkip = array ();
282
+ return $ res ;
272
283
}
273
284
274
285
/**
@@ -292,7 +303,27 @@ public function delete () {
292
303
return false ;
293
304
294
305
$ this ->db ->where ($ this ->primaryKey , $ this ->data [$ this ->primaryKey ]);
295
- return $ this ->db ->delete ($ this ->dbTable );
306
+ $ res = $ this ->db ->delete ($ this ->dbTable );
307
+ $ this ->toSkip = array ();
308
+ return $ res ;
309
+ }
310
+
311
+ /**
312
+ * chained method that append a field or fields to skipping
313
+ * @param mixed|array|false $field field name; array of names; empty skipping if false
314
+ * @return $this
315
+ */
316
+ public function skip ($ field ){
317
+ if (is_array ($ field )) {
318
+ foreach ($ field as $ f ) {
319
+ $ this ->toSkip [] = $ f ;
320
+ }
321
+ } else if ($ field === false ) {
322
+ $ this ->toSkip = array ();
323
+ } else {
324
+ $ this ->toSkip [] = $ field ;
325
+ }
326
+ return $ this ;
296
327
}
297
328
298
329
/**
@@ -618,6 +649,9 @@ private function validate ($data) {
618
649
return true ;
619
650
620
651
foreach ($ this ->dbFields as $ key => $ desc ) {
652
+ if (in_array ($ key , $ this ->toSkip ))
653
+ continue ;
654
+
621
655
$ type = null ;
622
656
$ required = false ;
623
657
if (isset ($ data [$ key ]))
@@ -684,6 +718,9 @@ private function prepareData () {
684
718
return $ this ->data ;
685
719
686
720
foreach ($ this ->data as $ key => &$ value ) {
721
+ if (in_array ($ key , $ this ->toSkip ))
722
+ continue ;
723
+
687
724
if ($ value instanceof dbObject && $ value ->isNew == true ) {
688
725
$ id = $ value ->save ();
689
726
if ($ id )
0 commit comments