Skip to content

Commit

Permalink
Merge branch 'master' into 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Dec 11, 2024
2 parents 8649070 + 23a3b94 commit af9efb0
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 43 deletions.
17 changes: 11 additions & 6 deletions packages/di/src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static function getDefaultAttributes(): array
*/
public function set(string $id, mixed $value, int $options = 0): StoreDefinitionInterface
{
$definition = $this->getDefinition($id);
$definition = $this->findDefinition($id, false);

if ($definition && $definition->isProtected()) {
throw new DefinitionException(
Expand Down Expand Up @@ -380,7 +380,7 @@ public function has(string $id): bool

public function hasCached(string $id): bool
{
return $this->getDefinition($id)?->getCache() !== null;
return $this->findDefinition($id, false)?->getCache() !== null;
}

/**
Expand Down Expand Up @@ -416,7 +416,7 @@ public function remove(string $id): static
*/
public function fork(string $id, string $newId, bool $forceNew = false): mixed
{
$raw = clone $this->getDefinition($id);
$raw = clone $this->findDefinition($id, true);

$this->storage[$newId] = $raw;

Expand All @@ -433,6 +433,11 @@ public function fork(string $id, string $newId, bool $forceNew = false): mixed
* @since 2.0
*/
public function getDefinition(string $id): ?StoreDefinitionInterface
{
return $this->findDefinition($id, true);
}

protected function findDefinition(string $id, bool $serviceAutowire = false): ?StoreDefinitionInterface
{
$id = $this->resolveAlias($id);

Expand All @@ -441,7 +446,7 @@ public function getDefinition(string $id): ?StoreDefinitionInterface
}

// Get instant service
if (class_exists($id) && $service = static::getServiceAttribute(new ReflectionClass($id))) {
if ($serviceAutowire && class_exists($id) && $service = static::getServiceAttribute(new ReflectionClass($id))) {
$definition = new StoreDefinition($id, $this->newInstance($id));
$definition->providedIn($service->providedIn);

Expand All @@ -451,7 +456,7 @@ public function getDefinition(string $id): ?StoreDefinitionInterface
}

if ($this->parent instanceof static) {
$parentDefinition = $this->parent->getDefinition($id);
$parentDefinition = $this->parent->findDefinition($id, $serviceAutowire);

// Store parent definition as self
if ($parentDefinition) {
Expand Down Expand Up @@ -481,7 +486,7 @@ public function clear(): void
public function clearCache(?string $id = null): void
{
if ($id !== null) {
$this->getDefinition($id)?->reset();
$this->findDefinition($id, false)?->reset();

return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/orm/src/Attributes/CreatedTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function filter(mixed $value): DateTimeImmutable

protected function getDefaultCaster(): callable
{
return function (mixed $value, ORM $orm, object $entity) {
return function (mixed $value, ORM $orm, object $entity, bool $isNew = false) {
$isNull = $value === null || $orm->getDb()->isNullDate($value);

$mapper = $orm->mapper($entity::class);

if ($isNull) {
if ($mapper->getMainKey()) {
if ($mapper->isNew($entity)) {
if ($isNew) {
$value = $this->getCurrent();
}
} else {
Expand Down
13 changes: 13 additions & 0 deletions packages/orm/src/Attributes/UseRealColumns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Windwalker\ORM\Attributes;

/**
* Use real DB columns when Query auto-join and prepare columns.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class UseRealColumns
{
}
5 changes: 3 additions & 2 deletions packages/orm/src/Cast/CastManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function castToCallback(mixed $cast, int $options, string $direction = 'h

public function wrapCastCallback(callable $caster, int $options): \Closure
{
return function (mixed $value, ORM $orm, ?object $entity = null) use ($options, $caster) {
return function (mixed $value, ORM $orm, ?object $entity = null, bool $isNew = false) use ($options, $caster) {
if ($value === '' && ($options & Cast::EMPTY_STRING_TO_NULL)) {
$value = null;
}
Expand All @@ -252,7 +252,8 @@ public function wrapCastCallback(callable $caster, int $options): \Closure
$value,
'value' => $value,
'orm' => $orm,
'entity' => $entity
'entity' => $entity,
'isNew' => $isNew
]
);
};
Expand Down
61 changes: 33 additions & 28 deletions packages/orm/src/EntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
BeforeSaveEvent,
BeforeStoreEvent,
BeforeUpdateWhereEvent,
EnergizeEvent};
EnergizeEvent
};
use Windwalker\ORM\Hydrator\EntityHydrator;
use Windwalker\ORM\Iterator\ResultIterator;
use Windwalker\ORM\Metadata\EntityMetadata;
Expand Down Expand Up @@ -229,9 +230,9 @@ public function findColumn(string $column, mixed $conditions = []): Collection
}

/**
* @param string $column
* @param Conditions $conditions
* @param array|string|null $groups
* @param string $column
* @param Conditions $conditions
* @param array|string|null $groups
*
* @return int
*/
Expand All @@ -248,9 +249,9 @@ public function countColumn(string $column, mixed $conditions = [], array|string
}

/**
* @param string $column
* @param Conditions $conditions
* @param array|string|null $groups
* @param string $column
* @param Conditions $conditions
* @param array|string|null $groups
*
* @return float
*/
Expand Down Expand Up @@ -304,7 +305,7 @@ public function createOne(array|object $source = [], int $options = 0): object
$extra = $event->getExtra();
$entity = $this->hydrate($fullData, $this->toEntity($source));

$data = $this->castForSave($this->extract($entity), true, $entity);
$data = $this->castForSave($this->extract($entity), true, $entity, true);

$type = BeforeStoreEvent::TYPE_CREATE;
$event = $this->emitEvent(
Expand Down Expand Up @@ -502,9 +503,9 @@ public function updateOne(
/**
* updateMultiple
*
* @param iterable $items
* @param array|string|null $condFields
* @param int $options
* @param iterable $items
* @param array|string|null $condFields
* @param int $options
*
* @return StatementInterface[]
*/
Expand Down Expand Up @@ -591,9 +592,9 @@ public function updateBatch(array|object $data, mixed $conditions = null, int $o
}

/**
* @param iterable $items
* @param string|array|null $condFields
* @param int $options
* @param iterable $items
* @param string|array|null $condFields
* @param int $options
*
* @return iterable<T>
*
Expand Down Expand Up @@ -670,9 +671,9 @@ public function isNew(array|object $item): bool
}

/**
* @param array|object $item
* @param array|string|null $condFields
* @param int $options
* @param array|object $item
* @param array|string|null $condFields
* @param int $options
*
* @return object|T
*
Expand Down Expand Up @@ -899,9 +900,9 @@ public function flush(iterable $items, mixed $conditions = [], int $options = 0)
}

/**
* @param Conditions $conditions
* @param callable|iterable|null $newValue
* @param int $options
* @param Conditions $conditions
* @param callable|iterable|null $newValue
* @param int $options
*
* @return array<T>
* @throws JsonException
Expand Down Expand Up @@ -1435,16 +1436,20 @@ protected function handleConditionColumn(string $key, mixed $value): mixed
return $value;
}

protected function extractForSave(object|array $data, bool $updateNulls = true): array
protected function extractForSave(object|array $data, bool $updateNulls = true, bool $isNew = false): array
{
$data = $this->extract($data);
$entity = $this->toEntity($data);

return $this->castForSave($data, $updateNulls, $entity);
return $this->castForSave($data, $updateNulls, $entity, $isNew);
}

protected function castForSave(array $data, bool $updateNulls = true, ?object $entity = null): array
{
protected function castForSave(
array $data,
bool $updateNulls = true,
?object $entity = null,
bool $isNew = false
): array {
$entity ??= $this->toEntity($data);

$metadata = $this->getMetadata();
Expand All @@ -1459,7 +1464,7 @@ protected function castForSave(array $data, bool $updateNulls = true, ?object $e

// Handler property attributes
if ($prop = $metadata->getColumn($field)?->getProperty()) {
$value = $this->castProperty($prop, $value, $entity);
$value = $this->castProperty($prop, $value, $entity, $isNew);
}

if (!$updateNulls && $value === null) {
Expand Down Expand Up @@ -1514,20 +1519,20 @@ protected function castForSave(array $data, bool $updateNulls = true, ?object $e
return $item;
}

protected function castProperty(ReflectionProperty $prop, mixed $value, object $entity): mixed
protected function castProperty(ReflectionProperty $prop, mixed $value, object $entity, bool $isNew = false): mixed
{
$castManager = $this->getMetadata()->getCastManager();

AttributesAccessor::runAttributeIfExists(
$prop,
CastForSave::class,
function (CastForSave $attr) use ($entity, $castManager, &$value) {
function (CastForSave $attr) use ($isNew, $entity, $castManager, &$value) {
$caster = $castManager->wrapCastCallback(
$castManager->castToCallback($attr->getCaster() ?? $attr, $attr->options ?? 0),
$attr->options
);

$value = $caster($value, $this->getORM(), $entity);
$value = $caster($value, $this->getORM(), $entity, $isNew);
},
ReflectionAttribute::IS_INSTANCEOF
);
Expand Down
16 changes: 11 additions & 5 deletions packages/orm/src/SelectorQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Windwalker\ORM;

use InvalidArgumentException;
use Windwalker\Attributes\AttributesAccessor;
use Windwalker\Data\Collection;
use Windwalker\Database\DatabaseAdapter;
use Windwalker\Database\Event\HydrateEvent;
use Windwalker\Database\Event\ItemFetchedEvent;
use Windwalker\Event\EventAwareInterface;
use Windwalker\Event\EventAwareTrait;
use Windwalker\ORM\Attributes\UseRealColumns;
use Windwalker\ORM\Attributes\Mapping;
use Windwalker\ORM\Metadata\EntityMetadata;
use Windwalker\ORM\Relation\Strategy\ManyToMany;
Expand Down Expand Up @@ -112,12 +114,16 @@ public function autoSelections(string $divider = '.', ?array &$columns = null):

$tableName = static::convertClassToTable($className, $alias);

if (class_exists($className)) {
$cols = array_keys($this->orm->getEntityMetadata($className)->getPureColumns());
} else {
$tbm = $db->getTable($tableName);
$loadColsFromDb = !class_exists($className)
|| (
class_exists($className)
&& AttributesAccessor::getFirstAttribute($className, UseRealColumns::class)
);

$cols = $tbm->getColumnNames();
if ($loadColsFromDb) {
$cols = $db->getTable($tableName)->getColumnNames();
} else {
$cols = array_keys($this->orm->getEntityMetadata($className)->getPureColumns());
}

foreach ($cols as $col) {
Expand Down

0 comments on commit af9efb0

Please sign in to comment.