Skip to content

one can skip some fields during validation & save #731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions dbObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Alexander V. Butenko <[email protected]>
* @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 ()
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}

Expand All @@ -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))
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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)
Expand Down