Compare commits
7 Commits
e37035eed8
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b8dd1fd0f6 | |||
| 83c80bd4ab | |||
| b13c07d3c4 | |||
| b95747bfe0 | |||
| 01c5eceeb4 | |||
| ee9c109ed9 | |||
| 7eed69725d |
@@ -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.
|
||||||
*/
|
*/
|
||||||
@@ -36,7 +31,7 @@ class Model
|
|||||||
/**
|
/**
|
||||||
* @var array Attributes to ignore when saving to the database.
|
* @var array Attributes to ignore when saving to the database.
|
||||||
*/
|
*/
|
||||||
protected static array $dbIgnoreSave = ['id'];
|
protected static array $dbIgnoreSave = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array Attributes that should be explicitly saved, even if private/protected.
|
* @var array Attributes that should be explicitly saved, even if private/protected.
|
||||||
@@ -161,7 +156,19 @@ class Model
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$prepared = $db->prepare($query);
|
$prepared = $db->prepare($query);
|
||||||
$prepared->execute(static::$dbQueryVariables);
|
foreach (static::$dbQueryVariables as $key => $value) {
|
||||||
|
// Detect data type to choose the PDO constant
|
||||||
|
$type = match (gettype($value)) {
|
||||||
|
'integer' => PDO::PARAM_INT,
|
||||||
|
'boolean' => PDO::PARAM_BOOL,
|
||||||
|
'NULL' => PDO::PARAM_NULL,
|
||||||
|
default => PDO::PARAM_STR,
|
||||||
|
};
|
||||||
|
|
||||||
|
$prepared->bindValue($key, $value, $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
$prepared->execute();
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
if ($db->inTransaction()) {
|
if ($db->inTransaction()) {
|
||||||
$db->rollBack();
|
$db->rollBack();
|
||||||
@@ -293,25 +300,16 @@ class Model
|
|||||||
$instance = new $class();
|
$instance = new $class();
|
||||||
$reflection = new ReflectionClass($instance);
|
$reflection = new ReflectionClass($instance);
|
||||||
$properties = $reflection->getProperties();
|
$properties = $reflection->getProperties();
|
||||||
$propertyNames = array_map(function ($property) {
|
|
||||||
return static::camelCaseToSnakeCase($property->name);
|
$map = [];
|
||||||
}, $properties);
|
foreach ($properties as $prop) {
|
||||||
|
$map[static::camelCaseToSnakeCase($prop->name)] = $prop;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($elem as $key => $value) {
|
foreach ($elem as $key => $value) {
|
||||||
$index = array_search($key, $propertyNames);
|
if (isset($map[$key])) {
|
||||||
if (is_numeric($index)) {
|
$property = $map[$key];
|
||||||
$type = $properties[$index]->getType();
|
$instance->{$property->name} = static::castValue($property, $value);
|
||||||
if (enum_exists($type->getName())) {
|
|
||||||
if (method_exists($type->getName(), 'tryFrom')) {
|
|
||||||
$instance->{$properties[$index]->name} = $type->getName()::tryfrom($value);
|
|
||||||
} elseif (
|
|
||||||
in_array($value, array_column($type->getName()::cases(), 'name'))
|
|
||||||
) {
|
|
||||||
$instance->{$properties[$index]->name} = $type->getName()::{$value};
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$instance->{$properties[$index]->name} = $value;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$instance->{static::snakeCaseToCamelCase($key)} = $value;
|
$instance->{static::snakeCaseToCamelCase($key)} = $value;
|
||||||
}
|
}
|
||||||
@@ -322,6 +320,41 @@ class Model
|
|||||||
return $instance;
|
return $instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles type casting for properties
|
||||||
|
*
|
||||||
|
* @param ReflectionProperty $property
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected static function castValue(ReflectionProperty $property, mixed $value): mixed
|
||||||
|
{
|
||||||
|
$type = $property->getType();
|
||||||
|
if (!$type) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$typeName = $type->getName();
|
||||||
|
|
||||||
|
// Enum cast
|
||||||
|
if (enum_exists($typeName)) {
|
||||||
|
if (method_exists($typeName, 'tryFrom')) {
|
||||||
|
return $typeName::tryFrom($value);
|
||||||
|
}
|
||||||
|
if (in_array($value, array_column($typeName::cases(), 'name'))) {
|
||||||
|
return $typeName::{$value};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DateTime cast
|
||||||
|
if (is_subclass_of($typeName, \DateTimeInterface::class)) {
|
||||||
|
return $value ? new $typeName($value) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the attributes to be saved for the current class.
|
* Returns the attributes to be saved for the current class.
|
||||||
* Attributes will be those that are public and not excluded in static::$ignoreSave,
|
* Attributes will be those that are public and not excluded in static::$ignoreSave,
|
||||||
@@ -337,30 +370,43 @@ 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)) {
|
||||||
$result[$this->camelCaseToSnakeCase($property->name)] = isset($this->{$property->name})
|
$name = $this->camelCaseToSnakeCase($property->name);
|
||||||
? $this->{$property->name} : null;
|
$result[$name] = $this->castToDbValue($this->{$property->name});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (static::$dbForceSave as $value) {
|
foreach (static::$dbForceSave as $value) {
|
||||||
$result[$value] = isset($this->$value)
|
$result[$value] = $this->castToDbValue($this->$value);
|
||||||
? $this->$value : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($result as $i => $property) {
|
|
||||||
if (gettype($property) == 'boolean') {
|
|
||||||
$result[$i] = $property ? '1' : '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($property instanceof \UnitEnum) {
|
|
||||||
$result[$i] = $property->value ?? $property->name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a PHP value to a database-compatible value.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function castToDbValue(mixed $value): mixed
|
||||||
|
{
|
||||||
|
if ($value instanceof \UnitEnum) {
|
||||||
|
return $value->value ?? $value->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($value instanceof \DateTimeInterface) {
|
||||||
|
return $value->format('Y-m-d H:i:s');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the name of the current class, even if it's an extended class.
|
* Returns the name of the current class, even if it's an extended class.
|
||||||
*
|
*
|
||||||
@@ -432,25 +478,18 @@ 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 (in_array($key, $this->dbSetToNull)) {
|
|
||||||
$set[] = "$key=NULL";
|
|
||||||
} else {
|
|
||||||
$set[] = "$key=:$key";
|
$set[] = "$key=:$key";
|
||||||
static::$dbQueryVariables[':' . $key] = $value;
|
static::$dbQueryVariables[':' . $key] = $value;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (in_array($key, $this->dbSetToNull)) {
|
|
||||||
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -468,19 +507,17 @@ class Model
|
|||||||
static::$dbQueryVariables = [];
|
static::$dbQueryVariables = [];
|
||||||
|
|
||||||
foreach ($atts as $key => $value) {
|
foreach ($atts as $key => $value) {
|
||||||
if (isset($value)) {
|
|
||||||
$into[] = "$key";
|
$into[] = "$key";
|
||||||
$values[] = ":$key";
|
$values[] = ":$key";
|
||||||
static::$dbQueryVariables[":$key"] = $value;
|
static::$dbQueryVariables[":$key"] = $value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$table = static::table();
|
$table = static::table();
|
||||||
$sql = "INSERT INTO $table (" . join(', ', $into) . ") VALUES (" . join(', ', $values) . ")";
|
$sql = "INSERT INTO $table (" . join(', ', $into) . ") VALUES (" . join(', ', $values) . ")";
|
||||||
static::query($sql);
|
static::query($sql);
|
||||||
|
|
||||||
$pk = static::$dbPrimaryKey;
|
$pk = static::$dbPrimaryKey;
|
||||||
$this->$pk = $db->lastInsertId();
|
$this->$pk ??= $db->lastInsertId();
|
||||||
$this->markAsSaved();
|
$this->markAsSaved();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1145,22 +1182,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