fix(model): improve PDO parameter binding and update logic
This commit is contained in:
@@ -23,11 +23,6 @@ use ReflectionProperty;
|
|||||||
#[AllowDynamicProperties]
|
#[AllowDynamicProperties]
|
||||||
class Model
|
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.
|
* @var string The name of the primary key column.
|
||||||
*/
|
*/
|
||||||
@@ -161,7 +156,23 @@ class Model
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$prepared = $db->prepare($query);
|
$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) {
|
} catch (PDOException $e) {
|
||||||
if ($db->inTransaction()) {
|
if ($db->inTransaction()) {
|
||||||
$db->rollBack();
|
$db->rollBack();
|
||||||
@@ -354,6 +365,11 @@ class Model
|
|||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
foreach ($properties as $property) {
|
foreach ($properties as $property) {
|
||||||
|
// Avoid save unset variables
|
||||||
|
if ($property->getType() && !$property->isInitialized($this)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (!in_array($property->name, static::$dbIgnoreSave)) {
|
if (!in_array($property->name, static::$dbIgnoreSave)) {
|
||||||
$name = $this->camelCaseToSnakeCase($property->name);
|
$name = $this->camelCaseToSnakeCase($property->name);
|
||||||
$result[$name] = $this->castToDbValue($this->{$property->name});
|
$result[$name] = $this->castToDbValue($this->{$property->name});
|
||||||
@@ -375,10 +391,6 @@ class Model
|
|||||||
*/
|
*/
|
||||||
protected function castToDbValue(mixed $value): mixed
|
protected function castToDbValue(mixed $value): mixed
|
||||||
{
|
{
|
||||||
if (is_bool($value)) {
|
|
||||||
return $value ? '1' : '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($value instanceof \UnitEnum) {
|
if ($value instanceof \UnitEnum) {
|
||||||
return $value->value ?? $value->name;
|
return $value->value ?? $value->name;
|
||||||
}
|
}
|
||||||
@@ -457,25 +469,22 @@ class Model
|
|||||||
$atts = $this->getVars();
|
$atts = $this->getVars();
|
||||||
static::$dbQueryVariables = [];
|
static::$dbQueryVariables = [];
|
||||||
|
|
||||||
|
$set = [];
|
||||||
foreach ($atts as $key => $value) {
|
foreach ($atts as $key => $value) {
|
||||||
if (isset($value)) {
|
if ($value !== null) {
|
||||||
if (in_array($key, $this->dbSetToNull)) {
|
|
||||||
$set[] = "$key=NULL";
|
|
||||||
} else {
|
|
||||||
$set[] = "$key=:$key";
|
$set[] = "$key=:$key";
|
||||||
static::$dbQueryVariables[':' . $key] = $value;
|
static::$dbQueryVariables[':' . $key] = $value;
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (in_array($key, $this->dbSetToNull)) {
|
|
||||||
$set[] = "$key=NULL";
|
$set[] = "$key=NULL";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$table = static::table();
|
$table = static::table();
|
||||||
$pk = static::$dbPrimaryKey;
|
$pk = static::$dbPrimaryKey;
|
||||||
$pkv = $this->$pk;
|
$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);
|
static::query($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1170,22 +1179,4 @@ class Model
|
|||||||
|
|
||||||
return $instances;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user