diff --git a/src/Libs/Model.php b/src/Libs/Model.php index 5fda117..d6d8c8f 100644 --- a/src/Libs/Model.php +++ b/src/Libs/Model.php @@ -23,11 +23,6 @@ use ReflectionProperty; #[AllowDynamicProperties] class Model { - /** - * @var array Attributes that should be set to NULL on update. - */ - protected array $dbSetToNull = []; - /** * @var string The name of the primary key column. */ @@ -161,7 +156,23 @@ class Model try { $prepared = $db->prepare($query); - $prepared->execute(static::$dbQueryVariables); + foreach (static::$dbQueryVariables as $key => $value) { + // Detectar el tipo de dato para elegir la constante de PDO + $type = PDO::PARAM_STR; // Por defecto string + if (is_int($value)) { + $type = PDO::PARAM_INT; + } + if (is_bool($value)) { + $type = PDO::PARAM_BOOL; + } + if (is_null($value)) { + $type = PDO::PARAM_NULL; + } + + $prepared->bindValue($key, $value, $type); + } + + $prepared->execute(); } catch (PDOException $e) { if ($db->inTransaction()) { $db->rollBack(); @@ -354,6 +365,11 @@ class Model $result = []; foreach ($properties as $property) { + // Avoid save unset variables + if ($property->getType() && !$property->isInitialized($this)) { + continue; + } + if (!in_array($property->name, static::$dbIgnoreSave)) { $name = $this->camelCaseToSnakeCase($property->name); $result[$name] = $this->castToDbValue($this->{$property->name}); @@ -375,10 +391,6 @@ class Model */ protected function castToDbValue(mixed $value): mixed { - if (is_bool($value)) { - return $value ? '1' : '0'; - } - if ($value instanceof \UnitEnum) { return $value->value ?? $value->name; } @@ -457,25 +469,22 @@ class Model $atts = $this->getVars(); static::$dbQueryVariables = []; + $set = []; foreach ($atts as $key => $value) { - if (isset($value)) { - if (in_array($key, $this->dbSetToNull)) { - $set[] = "$key=NULL"; - } else { - $set[] = "$key=:$key"; - static::$dbQueryVariables[':' . $key] = $value; - } + if ($value !== null) { + $set[] = "$key=:$key"; + static::$dbQueryVariables[':' . $key] = $value; } else { - if (in_array($key, $this->dbSetToNull)) { - $set[] = "$key=NULL"; - } + $set[] = "$key=NULL"; } } $table = static::table(); $pk = static::$dbPrimaryKey; $pkv = $this->$pk; - $sql = "UPDATE $table SET " . join(', ', $set) . " WHERE $pk='$pkv'"; + $sql = "UPDATE $table SET " . join(', ', $set) . " WHERE $pk=:$pk"; + static::$dbQueryVariables[":$pk"] = $pkv; + static::query($sql); } @@ -1170,22 +1179,4 @@ class Model return $instances; } - - /** - * Allows defining an attribute's value as null. - * Only works for updating an element in the DB, not for inserting. - * - * @param array $attributes - * Attribute or array of attributes that will be set to null. - * - * @return void - */ - public function setNull(string ...$attributes): void - { - foreach ($attributes as $att) { - if (!in_array($att, $this->dbSetToNull)) { - $this->dbSetToNull[] = $att; - } - } - } }