Compare commits
4 Commits
01c5eceeb4
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b8dd1fd0f6 | |||
| 83c80bd4ab | |||
| b13c07d3c4 | |||
| b95747bfe0 |
@@ -31,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.
|
||||||
@@ -321,7 +321,7 @@ class Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles type casting for properties, especially for Enums.
|
* Handles type casting for properties
|
||||||
*
|
*
|
||||||
* @param ReflectionProperty $property
|
* @param ReflectionProperty $property
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
@@ -331,16 +331,25 @@ class Model
|
|||||||
protected static function castValue(ReflectionProperty $property, mixed $value): mixed
|
protected static function castValue(ReflectionProperty $property, mixed $value): mixed
|
||||||
{
|
{
|
||||||
$type = $property->getType();
|
$type = $property->getType();
|
||||||
if (!$type || !enum_exists($typeName = $type->getName())) {
|
if (!$type) {
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method_exists($typeName, 'tryFrom')) {
|
$typeName = $type->getName();
|
||||||
return $typeName::tryFrom($value);
|
|
||||||
|
// 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};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (in_array($value, array_column($typeName::cases(), 'name'))) {
|
// DateTime cast
|
||||||
return $typeName::{$value};
|
if (is_subclass_of($typeName, \DateTimeInterface::class)) {
|
||||||
|
return $value ? new $typeName($value) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
@@ -391,6 +400,10 @@ class Model
|
|||||||
return $value->value ?? $value->name;
|
return $value->value ?? $value->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($value instanceof \DateTimeInterface) {
|
||||||
|
return $value->format('Y-m-d H:i:s');
|
||||||
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -467,12 +480,8 @@ class Model
|
|||||||
|
|
||||||
$set = [];
|
$set = [];
|
||||||
foreach ($atts as $key => $value) {
|
foreach ($atts as $key => $value) {
|
||||||
if ($value !== null) {
|
$set[] = "$key=:$key";
|
||||||
$set[] = "$key=:$key";
|
static::$dbQueryVariables[':' . $key] = $value;
|
||||||
static::$dbQueryVariables[':' . $key] = $value;
|
|
||||||
} else {
|
|
||||||
$set[] = "$key=NULL";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$table = static::table();
|
$table = static::table();
|
||||||
@@ -498,11 +507,9 @@ 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();
|
||||||
@@ -510,7 +517,7 @@ class Model
|
|||||||
static::query($sql);
|
static::query($sql);
|
||||||
|
|
||||||
$pk = static::$dbPrimaryKey;
|
$pk = static::$dbPrimaryKey;
|
||||||
$this->$pk = $db->lastInsertId();
|
$this->$pk ??= $db->lastInsertId();
|
||||||
$this->markAsSaved();
|
$this->markAsSaved();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user