Skip to content

Commit

Permalink
uuid primary key support
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Aug 2, 2021
1 parent 1fa6c0e commit d4e8edb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ public function find($params = [], $one = false)
$params = $this->normalize($params);

if (array_key_exists('id', $params)) {
if (array_key_exists($params['id'], $this->persisted)) {
$instance = $this->persisted[$params['id']];
$key = (string) $params['id'];
if (array_key_exists($key, $this->persisted)) {
$instance = $this->persisted[$key];
return $one ? $instance : [$instance];
}
}
Expand Down Expand Up @@ -229,8 +230,14 @@ public function find($params = [], $one = false)
return $this->results[$cacheKey] = $result;
}

public function forget(int $id): self
public function forget($id): self
{
if ($id instanceof Entity) {
$id = $this->space->getInstanceKey($id);
}

$id = (string) $id;

if (array_key_exists($id, $this->persisted)) {
$instance = $this->persisted[$id];
unset($this->keys[$instance]);
Expand All @@ -244,7 +251,7 @@ public function forget(int $id): self

public function getInstance(array $tuple): Entity
{
$key = $this->space->getTupleKey($tuple);
$key = (string) $this->space->getTupleKey($tuple);

if (array_key_exists($key, $this->persisted)) {
return $this->persisted[$key];
Expand Down Expand Up @@ -326,7 +333,7 @@ public function remove($params = []): self

public function removeEntity(Entity $instance): self
{
$key = $this->space->getInstanceKey($instance);
$key = (string) $this->space->getInstanceKey($instance);

if (!array_key_exists($key, $this->original)) {
return $this;
Expand Down Expand Up @@ -372,7 +379,7 @@ public function removeEntity(Entity $instance): self

public function save(Entity $instance): Entity
{
$key = $this->space->getInstanceKey($instance);
$key = (string) $this->space->getInstanceKey($instance);
$client = $this->getMapper()->getClient();

if (array_key_exists($key, $this->persisted)) {
Expand Down Expand Up @@ -478,7 +485,7 @@ private function addDefaultValues(Entity $instance): Entity

public function getOriginal(Entity $instance): ?array
{
$key = $this->space->getInstanceKey($instance);
$key = (string) $this->space->getInstanceKey($instance);

if (array_key_exists($key, $this->original)) {
return $this->original[$key];
Expand Down
26 changes: 25 additions & 1 deletion tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,31 @@

class UuidTest extends TestCase
{
public function test()
public function testPrimaryKey()
{
$mapper = $this->createMapper();
$this->clean($mapper);

$mapper->getSchema()->createSpace('test_space', [
'is_sync' => true,
'engine' => 'memtx',
'properties' => [
'id' => 'uuid',
],
])
->addIndex([ 'fields' => 'id' ]);

$uuid = Uuid::v4();

$instance = $mapper->create('test_space', [
'id' => $uuid,
]);

$mapper->getRepository('test_space')->forget($instance);
$this->assertNotNull($mapper->findOne('test_space', $uuid));
}

public function testBasics()
{
$mapper = $this->createMapper();
$this->clean($mapper);
Expand Down

0 comments on commit d4e8edb

Please sign in to comment.