diff --git a/dbObject.php b/dbObject.php index 3f663ae2..dbf39d89 100644 --- a/dbObject.php +++ b/dbObject.php @@ -7,7 +7,7 @@ * @author Alexander V. Butenko * @copyright Copyright (c) 2015-2017 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - * @link http://github.com/joshcam/PHP-MySQLi-Database-Class + * @link http://github.com/joshcam/PHP-MySQLi-Database-Class * @version 2.9-master * * @method int count () @@ -106,6 +106,11 @@ class dbObject { */ protected $dbTable; + /** + * @var array name of the fields that will be skipped during validation, preparing & saving + */ + protected $toSkip = array(); + /** * @param array $data Data to preload on object creation */ @@ -241,7 +246,7 @@ public function insert () { if (!empty ($this->primaryKey) && empty ($this->data[$this->primaryKey])) $this->data[$this->primaryKey] = $id; $this->isNew = false; - + $this->toSkip = array(); return $id; } @@ -256,8 +261,12 @@ public function update ($data = null) { return false; if ($data) { - foreach ($data as $k => $v) - $this->$k = $v; + foreach ($data as $k => $v) { + if (in_array($k, $this->toSkip)) + continue; + + $this->$k = $v; + } } if (!empty ($this->timestamps) && in_array ("updatedAt", $this->timestamps)) @@ -268,7 +277,9 @@ public function update ($data = null) { return false; $this->db->where ($this->primaryKey, $this->data[$this->primaryKey]); - return $this->db->update ($this->dbTable, $sqlData); + $res = $this->db->update ($this->dbTable, $sqlData); + $this->toSkip = array(); + return $res; } /** @@ -292,7 +303,27 @@ public function delete () { return false; $this->db->where ($this->primaryKey, $this->data[$this->primaryKey]); - return $this->db->delete ($this->dbTable); + $res = $this->db->delete ($this->dbTable); + $this->toSkip = array(); + return $res; + } + + /** + * chained method that append a field or fields to skipping + * @param mixed|array|false $field field name; array of names; empty skipping if false + * @return $this + */ + public function skip($field){ + if(is_array($field)) { + foreach ($field as $f) { + $this->toSkip[] = $f; + } + } else if($field === false) { + $this->toSkip = array(); + } else{ + $this->toSkip[] = $field; + } + return $this; } /** @@ -618,6 +649,9 @@ private function validate ($data) { return true; foreach ($this->dbFields as $key => $desc) { + if(in_array($key, $this->toSkip)) + continue; + $type = null; $required = false; if (isset ($data[$key])) @@ -684,6 +718,9 @@ private function prepareData () { return $this->data; foreach ($this->data as $key => &$value) { + if(in_array($key, $this->toSkip)) + continue; + if ($value instanceof dbObject && $value->isNew == true) { $id = $value->save(); if ($id)