Skip to content

Commit

Permalink
mapper: set persistedId right after entity processing [closes #360]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jul 7, 2020
1 parent bdfee30 commit 5d70e67
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 22 deletions.
33 changes: 21 additions & 12 deletions src/Mapper/Dbal/DbalMapper.php
Expand Up @@ -294,22 +294,15 @@ protected function createInflector(): IInflector

// == Persistence API ==============================================================================================


/**
* Returns persisted id.
* @return mixed
*/
public function persist(IEntity $entity)
public function persist(IEntity $entity): void
{
$this->beginTransaction();
$data = $this->entityToArray($entity);
$data = $this->getConventions()->convertEntityToStorage($data);

if (!$entity->isPersisted()) {
$this->processInsert($entity, $data);
return $entity->hasValue('id')
? $entity->getValue('id')
: $this->connection->getLastInsertedId($this->getConventions()->getPrimarySequenceName());

} else {
$primary = [];
$id = (array) $entity->getPersistedId();
Expand All @@ -319,7 +312,6 @@ public function persist(IEntity $entity)
$primary = $this->getConventions()->convertEntityToStorage($primary);

$this->processUpdate($entity, $data, $primary);
return $entity->getPersistedId();
}
}

Expand All @@ -334,6 +326,11 @@ protected function processInsert(IEntity $entity, array $data): void
$this->processAutoupdate($entity, $args);
} else {
$this->connection->queryArgs($args);

$id = $entity->hasValue('id')
? $entity->getValue('id')
: $this->connection->getLastInsertedId($this->getConventions()->getPrimarySequenceName());
$entity->onPersist($id);
}
}

Expand All @@ -353,6 +350,7 @@ protected function processUpdate(IEntity $entity, array $data, array $primary):
$this->processAutoupdate($entity, $args);
} else {
$this->connection->queryArgs($args);
$entity->onPersist($entity->getPersistedId());
}
}

Expand Down Expand Up @@ -382,6 +380,12 @@ protected function processPostgreAutoupdate(IEntity $entity, array $args): void
$args[] = 'RETURNING %ex';
$args[] = $this->getAutoupdateReselectExpression();
$row = $this->connection->queryArgs($args)->fetch();

$id = $entity->hasValue('id')
? $entity->getValue('id')
: $this->connection->getLastInsertedId($this->getConventions()->getPrimarySequenceName());
$entity->onPersist($id);

if ($row === null) {
$entity->onRefresh(null, true);
} else {
Expand All @@ -399,14 +403,19 @@ protected function processMySQLAutoupdate(IEntity $entity, array $args): void
assert($this instanceof IPersistAutoupdateMapper);
$this->connection->queryArgs($args);

$id = $entity->hasValue('id')
? $entity->getValue('id')
: $this->connection->getLastInsertedId();
$entity->onPersist($id);

$conventions = $this->getConventions();

$id = (array) $entity->getPersistedId();
$primary = [];
$id = (array) ($entity->isPersisted() ? $entity->getPersistedId() : $this->connection->getLastInsertedId());
foreach ($entity->getMetadata()->getPrimaryKey() as $key) {
$key = $conventions->convertEntityToStorageKey($key);
$primary[$key] = array_shift($id);
}
$primary = $this->getConventions()->convertEntityToStorage($primary);

$row = $this->connection->query(
'SELECT %ex FROM %table WHERE %and',
Expand Down
4 changes: 1 addition & 3 deletions src/Mapper/IMapper.php
Expand Up @@ -48,12 +48,10 @@ public function getRepository(): IRepository;


/**
* Persist entity and return new id.
* @return mixed
* @internal
* @see IRepository::persist()
*/
public function persist(IEntity $entity);
public function persist(IEntity $entity): void;


/**
Expand Down
5 changes: 3 additions & 2 deletions src/Mapper/Memory/ArrayMapper.php
Expand Up @@ -123,7 +123,7 @@ public function &getRelationshipDataStorage(string $key): array
}


public function persist(IEntity $entity)
public function persist(IEntity $entity): void
{
$this->initializeData();

Expand Down Expand Up @@ -157,7 +157,8 @@ public function persist(IEntity $entity)

$this->data[$primaryValue] = $entity;
$this->dataToStore[$primaryValue] = $data;
return $id;

$entity->onPersist($id);
}


Expand Down
3 changes: 1 addition & 2 deletions src/Repository/Repository.php
Expand Up @@ -421,8 +421,7 @@ public function doPersist(IEntity $entity): void
$this->onBeforeInsert($entity);
}

$id = $this->mapper->persist($entity);
$entity->onPersist($id);
$this->mapper->persist($entity);
$this->identityMap->add($entity);
$this->entitiesToFlush[0][] = $entity;

Expand Down
Expand Up @@ -37,12 +37,14 @@ class DbalPersistAutoupdateMapperTest extends DataTestCase
public function testInsertAndUpdate(): void
{
$bookCollection = new BookCollection();
$bookCollection->id = 99;
$bookCollection->name = 'Test Collection 1';

Assert::null($bookCollection->updatedAt);
$this->orm->bookColletions->persistAndFlush($bookCollection);

Assert::type(DateTimeImmutable::class, $bookCollection->updatedAt);
Assert::equal(99, $bookCollection->id);
$old = $bookCollection->updatedAt;

sleep(1);
Expand Down
2 changes: 1 addition & 1 deletion tests/db/mssql-init.sql
Expand Up @@ -94,7 +94,7 @@ CREATE TABLE contents

CREATE TABLE book_collections
(
id int NOT NULL IDENTITY (1,1),
id int NOT NULL,
name varchar(255) NOT NULL,
updated_at datetimeoffset NULL,
PRIMARY KEY (id)
Expand Down
2 changes: 1 addition & 1 deletion tests/db/mysql-init.sql
Expand Up @@ -94,7 +94,7 @@ CREATE TABLE contents

CREATE TABLE book_collections
(
id int UNSIGNED NOT NULL AUTO_INCREMENT,
id int UNSIGNED NOT NULL,
name varchar(255) NOT NULL,
updated_at datetime NULL,
PRIMARY KEY (`id`)
Expand Down
2 changes: 1 addition & 1 deletion tests/db/pgsql-init.sql
Expand Up @@ -94,7 +94,7 @@ CREATE TABLE "contents"

CREATE TABLE "book_collections"
(
"id" serial4 NOT NULL,
"id" int4 NOT NULL,
"name" varchar(255) NOT NULL,
"updated_at" timestamptz,
PRIMARY KEY ("id")
Expand Down

0 comments on commit 5d70e67

Please sign in to comment.