Added setNull method to ModelMysql.
This commit is contained in:
		| @@ -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; | ||||
|     } | ||||
|   } | ||||
| } | ||||
| ?> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user