Added setNull method to ModelMysql.

This commit is contained in:
kj 2020-10-24 04:47:39 -04:00
parent 831dd7ad47
commit cbd59c217f

View File

@ -17,6 +17,7 @@ use Libs\Database;
class ModelMySQL {
public $id;
protected $toNull = [];
static protected $primaryKey = 'id';
static protected $ignoreSave = ['id'];
@ -225,7 +226,13 @@ class ModelMySQL {
foreach ($atts as $key => $value) {
if (isset($value)) {
$value = static::db()->real_escape_string($value);
$set[]="$key='$value'";
if (in_array($key, $this->toNull))
$set[]="$key=NULL";
else
$set[]="$key='$value'";
} else {
if (in_array($key, $this->toNull))
$set[]="$key=NULL";
}
}
@ -712,5 +719,23 @@ class ModelMySQL {
return $instances;
}
/*
* Permite definir como nulo el valor de un atributo.
* Sólo funciona para actualizar un elemento de la BD, no para insertar.
*
* @param array $atts
*/
public function setNull($atts) {
if (!isset($this->id))
throw new \Exception(
"\nEl método setNull sólo funciona para actualizar, no al insertar."
);
foreach ($atts as $att) {
if (!in_array($att, $this->toNull))
$this->toNull[] = $att;
}
}
}
?>