Skip to content

Commit

Permalink
Fix for UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Oct 15, 2024
1 parent 3259d35 commit 492db23
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
19 changes: 11 additions & 8 deletions src/EntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Windwalker\Event\EventAwareTrait;
use Windwalker\Event\EventInterface;
use Windwalker\ORM\Attributes\CastForSave;
use Windwalker\ORM\Attributes\UUIDBin;
use Windwalker\ORM\Event\{AbstractSaveEvent,
AfterCopyEvent,
AfterDeleteEvent,
Expand All @@ -31,9 +32,7 @@
BeforeSaveEvent,
BeforeStoreEvent,
BeforeUpdateWhereEvent,
EnergizeEvent
};
use Windwalker\ORM\Attributes\UUIDBin;
EnergizeEvent};
use Windwalker\ORM\Hydrator\EntityHydrator;
use Windwalker\ORM\Iterator\ResultIterator;
use Windwalker\ORM\Metadata\EntityMetadata;
Expand All @@ -50,6 +49,7 @@
use function is_object;
use function Windwalker\collect;
use function Windwalker\raw;
use function Windwalker\try_wrap_uuid;

/**
* EntityMapper is an entity & database mapping object.
Expand Down Expand Up @@ -417,7 +417,7 @@ public function updateOne(
if (!($options & static::IGNORE_OLD_DATA) && $this->getKeys() && !empty($data[$this->getMainKey()])) {
$oldData = $this->getDb()->select('*')
->from($metadata->getTableName())
->where(Arr::only($data, $this->getKeys()))
->where($this->conditionsToWheres(Arr::only($data, $this->getKeys())))
->get()
?->dump();

Expand Down Expand Up @@ -554,7 +554,7 @@ public function updateWhere(array|object $source, mixed $conditions = null, int
$statement = $this->getDb()->getWriter()->updateWhere(
$metadata->getTableName(),
$data = $event->getData(),
$conditions = $event->getConditions()
$this->conditionsToWheres($conditions = $event->getConditions())
);

// Event
Expand Down Expand Up @@ -1015,7 +1015,10 @@ public function sync(iterable $items, mixed $conditions = [], ?array $compareKey

// Delete
foreach ($delItems as $k => $delItem) {
$this->deleteWhere(Arr::only($delItem, $compareKeys), $options);
$this->deleteWhere(
$this->conditionsToWheres(Arr::only($delItem, $compareKeys)),
$options
);

$delItems[$k] = $this->toEntity($delItem);
}
Expand Down Expand Up @@ -1423,10 +1426,10 @@ protected function handleConditionColumn(string $key, mixed $value): mixed

if ($uuidAttr) {
if (is_array($value)) {
return array_map(fn ($v) => new UuidBinWrapper($v), $value);
return array_map(fn($v) => try_wrap_uuid($v), $value);
}

return new UuidBinWrapper($value);
return try_wrap_uuid($value);
}

return $value;
Expand Down
6 changes: 5 additions & 1 deletion src/Metadata/EntityMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ public function __construct(string|object $entity, ORM $orm)
$this->addEventDealer($orm);
}

public static function isEntity(string|object $object): bool
public static function isEntity(string|object|array $object): bool
{
if (is_array($object)) {
return false;
}

$class = new ReflectionClass($object);

return $class->getAttributes(Table::class, ReflectionAttribute::IS_INSTANCEOF) !== [];
Expand Down
44 changes: 43 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Windwalker;

use Ramsey\Uuid\UuidInterface;
use ReflectionAttribute;
use UnexpectedValueException;
use Windwalker\ORM\Attributes\Table;
use Windwalker\Query\Wrapper\UuidBinWrapper;

if (!function_exists('entity_table')) {
if (!function_exists('\Windwalker\entity_table')) {
/**
* Get Table name from Entity object or class.
*
Expand Down Expand Up @@ -44,3 +46,43 @@ function entity_table(string|object $entity): string
return $entity;
}
}

if (!function_exists('\Windwalker\wrap_uuid')) {
function wrap_uuid(mixed $uuid): UuidBinWrapper {
return new UuidBinWrapper($uuid);
}
}

if (!function_exists('\Windwalker\try_wrap_uuid')) {
function try_wrap_uuid(mixed $uuid): ?UuidBinWrapper {
if ($uuid === null) {
return null;
}

return new UuidBinWrapper($uuid);
}
}

if (!function_exists('\Windwalker\to_uuid')) {
function to_uuid(mixed $uuid): UuidInterface {
return try_uuid($uuid);
}
}

if (!function_exists('\Windwalker\try_uuid')) {
function try_uuid(mixed $uuid): ?UuidInterface {
if ($uuid === null) {
return null;
}

if ($uuid instanceof UuidInterface) {
return $uuid;
}

if (is_string($uuid) && strlen($uuid) === 16) {
return Uuid::fromBytes($uuid);
}

return Uuid::fromString($uuid);
}
}

0 comments on commit 492db23

Please sign in to comment.