Commit e3497b7e authored by Ilya Goryachev's avatar Ilya Goryachev Committed by Alexander Butenko

one can skip some fields during validation & save (#731)

* one can skip some fields during validation & save

* reset skips after changes (save, delete)
parent e122f8e0
......@@ -7,7 +7,7 @@
* @author Alexander V. Butenko <a.butenka@gmail.com>
* @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 @@ class dbObject {
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 @@ class dbObject {
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 @@ class dbObject {
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 @@ class dbObject {
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 @@ class dbObject {
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 @@ class dbObject {
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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment