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
......@@ -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,9 +261,13 @@ class dbObject {
return false;
if ($data) {
foreach ($data as $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))
$this->updatedAt = date("Y-m-d H:i:s");
......@@ -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