Skip to content

Commit e3497b7

Browse files
ilyagoryavbdr
authored andcommitted
one can skip some fields during validation & save (#731)
* one can skip some fields during validation & save * reset skips after changes (save, delete)
1 parent e122f8e commit e3497b7

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

dbObject.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Alexander V. Butenko <[email protected]>
88
* @copyright Copyright (c) 2015-2017
99
* @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
1111
* @version 2.9-master
1212
*
1313
* @method int count ()
@@ -106,6 +106,11 @@ class dbObject {
106106
*/
107107
protected $dbTable;
108108

109+
/**
110+
* @var array name of the fields that will be skipped during validation, preparing & saving
111+
*/
112+
protected $toSkip = array();
113+
109114
/**
110115
* @param array $data Data to preload on object creation
111116
*/
@@ -241,7 +246,7 @@ public function insert () {
241246
if (!empty ($this->primaryKey) && empty ($this->data[$this->primaryKey]))
242247
$this->data[$this->primaryKey] = $id;
243248
$this->isNew = false;
244-
249+
$this->toSkip = array();
245250
return $id;
246251
}
247252

@@ -256,8 +261,12 @@ public function update ($data = null) {
256261
return false;
257262

258263
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+
}
261270
}
262271

263272
if (!empty ($this->timestamps) && in_array ("updatedAt", $this->timestamps))
@@ -268,7 +277,9 @@ public function update ($data = null) {
268277
return false;
269278

270279
$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;
272283
}
273284

274285
/**
@@ -292,7 +303,27 @@ public function delete () {
292303
return false;
293304

294305
$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;
296327
}
297328

298329
/**
@@ -618,6 +649,9 @@ private function validate ($data) {
618649
return true;
619650

620651
foreach ($this->dbFields as $key => $desc) {
652+
if(in_array($key, $this->toSkip))
653+
continue;
654+
621655
$type = null;
622656
$required = false;
623657
if (isset ($data[$key]))
@@ -684,6 +718,9 @@ private function prepareData () {
684718
return $this->data;
685719

686720
foreach ($this->data as $key => &$value) {
721+
if(in_array($key, $this->toSkip))
722+
continue;
723+
687724
if ($value instanceof dbObject && $value->isNew == true) {
688725
$id = $value->save();
689726
if ($id)

0 commit comments

Comments
 (0)