Skip to content

Commit

Permalink
Merge 80f8a27 into 5528c4f
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Nov 10, 2018
2 parents 5528c4f + 80f8a27 commit cc58281
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 29 deletions.
9 changes: 2 additions & 7 deletions src/Entity/AbstractEntity.php
Expand Up @@ -373,7 +373,7 @@ private function setterPrimaryProxy($value, PropertyMetadata $metadata)
}


private function getterPrimaryProxy($value = null, PropertyMetadata $metadata)
private function getterPrimaryProxy($value, PropertyMetadata $metadata)
{
if ($this->persistedId !== null) {
return $this->persistedId;
Expand All @@ -384,12 +384,7 @@ private function getterPrimaryProxy($value = null, PropertyMetadata $metadata)
$id = [];
$keys = $this->getMetadata()->getPrimaryKey();
foreach ($keys as $key) {
$value = $this->getRawValue($key);
if ($value instanceof \DateTimeImmutable) {
$id[] = $value->format('c.u');
} else {
$id[] = $value;
}
$id[] = $this->getRawValue($key);
}
if (count($keys) === 1) {
return $id[0];
Expand Down
2 changes: 1 addition & 1 deletion src/Mapper/Dbal/DbalMapper.php
Expand Up @@ -264,9 +264,9 @@ public function persist(IEntity $entity)
$primary = [];
$id = (array) $entity->getPersistedId();
foreach ($entity->getMetadata()->getPrimaryKey() as $key) {
$key = $this->storageReflection->convertEntityToStorageKey($key);
$primary[$key] = array_shift($id);
}
$primary = $this->getStorageReflection()->convertEntityToStorage($primary);

$this->processUpdate($entity, $data, $primary);
return $entity->getPersistedId();
Expand Down
32 changes: 25 additions & 7 deletions src/Mapper/Memory/ArrayMapper.php
Expand Up @@ -23,9 +23,6 @@
use Nextras\Orm\StorageReflection\CommonReflection;


/**
* Array Mapper.
*/
abstract class ArrayMapper extends BaseMapper
{
/** @var IEntity[]|null[]|null */
Expand Down Expand Up @@ -118,7 +115,7 @@ public function persist(IEntity $entity)

if ($entity->isPersisted()) {
$id = $entity->getPersistedId();
$primaryValue = implode(',', (array) $id);
$primaryValue = $this->getIdHash($id);

} else {
$this->lock();
Expand All @@ -131,7 +128,7 @@ public function persist(IEntity $entity)
} else {
$id = $entity->getValue('id');
}
$primaryValue = implode(',', (array) $id);
$primaryValue = $this->getIdHash($id);
if (isset($storedData[$primaryValue])) {
throw new InvalidStateException("Unique constraint violation: entity with '$primaryValue' primary value already exists.");
}
Expand All @@ -151,7 +148,7 @@ public function persist(IEntity $entity)
public function remove(IEntity $entity)
{
$this->initializeData();
$id = implode(',', (array) $entity->getPersistedId());
$id = $this->getIdHash($entity->getPersistedId());
$this->data[$id] = null;
$this->dataToStore[$id] = null;
}
Expand Down Expand Up @@ -203,7 +200,8 @@ protected function initializeData()

$entity = $repository->hydrateEntity($storageReflection->convertStorageToEntity($row));
if ($entity !== null) { // entity may have been deleted
$this->data[implode(',', (array) $entity->getPersistedId())] = $entity;
$idHash = $this->getIdHash($entity->getPersistedId());
$this->data[$idHash] = $entity;
}
}
}
Expand Down Expand Up @@ -290,6 +288,26 @@ protected function saveEntityData(array $data)
}


protected function getIdHash($id): string
{
if (!is_array($id)) {
return (string) $id;
}

return implode(
',',
array_map(
function ($id) {
return $id instanceof \DateTimeImmutable
? $id->format('c.u')
: $id;
},
$id
)
);
}


/**
* Reads stored data
*/
Expand Down
44 changes: 33 additions & 11 deletions src/Repository/IdentityMap.php
Expand Up @@ -41,7 +41,8 @@ public function __construct(IRepository $repository)
*/
public function hasById($id): bool
{
return isset($this->entities[implode(',', (array) $id)]);
$idHash = $this->getIdHash($id);
return isset($this->entities[$idHash]);
}


Expand All @@ -51,12 +52,12 @@ public function hasById($id): bool
*/
public function getById($id)
{
$id = implode(',', (array) $id);
if (!isset($this->entities[$id])) {
$idHash = $this->getIdHash($id);
if (!isset($this->entities[$idHash])) {
return null;
}

$entity = $this->entities[$id];
$entity = $this->entities[$idHash];
if ($entity instanceof IEntityHasPreloadContainer) {
$entity->setPreloadContainer(null);
}
Expand All @@ -66,7 +67,8 @@ public function getById($id)

public function add(IEntity $entity)
{
$this->entities[implode(',', (array) $entity->getPersistedId())] = $entity;
$id = $this->getIdHash($entity->getPersistedId());
$this->entities[$id] = $entity;
}


Expand All @@ -75,16 +77,16 @@ public function add(IEntity $entity)
*/
public function remove($id)
{
$id = implode(',', (array) $id);
$this->entities[$id] = false;
unset($this->entitiesForRefresh[$id]);
$idHash = $this->getIdHash($id);
$this->entities[$idHash] = false;
unset($this->entitiesForRefresh[$idHash]);
}


public function create(array $data): ?IEntity
{
$entity = $this->createEntity($data);
$id = implode(',', (array) $entity->getPersistedId());
$id = $this->getIdHash($entity->getPersistedId());

if (isset($this->entities[$id])) {
$this->repository->detach($entity);
Expand Down Expand Up @@ -135,14 +137,14 @@ public function destroyAllEntities()

public function markForRefresh(IEntity $entity)
{
$id = implode(',', (array) $entity->getPersistedId());
$id = $this->getIdHash($entity->getPersistedId());
$this->entitiesForRefresh[$id] = true;
}


public function isMarkedForRefresh(IEntity $entity): bool
{
$id = implode(',', (array) $entity->getPersistedId());
$id = $this->getIdHash($entity->getPersistedId());
return isset($this->entitiesForRefresh[$id]);
}

Expand All @@ -161,4 +163,24 @@ protected function createEntity(array $data): IEntity
$entity->onLoad($data);
return $entity;
}


protected function getIdHash($id): string
{
if (!is_array($id)) {
return (string) $id;
}

return implode(
',',
array_map(
function ($id) {
return $id instanceof \DateTimeImmutable
? $id->format('c.u')
: $id;
},
$id
)
);
}
}
14 changes: 11 additions & 3 deletions tests/cases/integration/Entity/entity.compositePK.phpt
Expand Up @@ -32,16 +32,24 @@ class EntityCompositePKTest extends DataTestCase
$user = new User();
$this->orm->persistAndFlush($user);

$at = new \DateTimeImmutable('2018-09-09 10:09:02');

$stat = new UserStat();
$stat->user = $user;
$stat->date = 'now';
$stat->date = $at;
$stat->value = 100;
$this->orm->persistAndFlush($stat);

$userId = $user->id;

$this->orm->clear();

$this->orm->userStats->findAll()->fetchAll();
Environment::$checkAssertions = false;
$userStat = $this->orm->userStats->getBy(['user' => $userId, 'date' => $at]);
Assert::true($userStat !== null);
Assert::type(\DateTimeImmutable::class, $userStat->id[1]);

$userStat->value = 101;
$this->orm->persistAndFlush($userStat);
}


Expand Down

0 comments on commit cc58281

Please sign in to comment.