Skip to content

Commit d4e8edb

Browse files
committed
uuid primary key support
1 parent 1fa6c0e commit d4e8edb

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/Repository.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,9 @@ public function find($params = [], $one = false)
189189
$params = $this->normalize($params);
190190

191191
if (array_key_exists('id', $params)) {
192-
if (array_key_exists($params['id'], $this->persisted)) {
193-
$instance = $this->persisted[$params['id']];
192+
$key = (string) $params['id'];
193+
if (array_key_exists($key, $this->persisted)) {
194+
$instance = $this->persisted[$key];
194195
return $one ? $instance : [$instance];
195196
}
196197
}
@@ -229,8 +230,14 @@ public function find($params = [], $one = false)
229230
return $this->results[$cacheKey] = $result;
230231
}
231232

232-
public function forget(int $id): self
233+
public function forget($id): self
233234
{
235+
if ($id instanceof Entity) {
236+
$id = $this->space->getInstanceKey($id);
237+
}
238+
239+
$id = (string) $id;
240+
234241
if (array_key_exists($id, $this->persisted)) {
235242
$instance = $this->persisted[$id];
236243
unset($this->keys[$instance]);
@@ -244,7 +251,7 @@ public function forget(int $id): self
244251

245252
public function getInstance(array $tuple): Entity
246253
{
247-
$key = $this->space->getTupleKey($tuple);
254+
$key = (string) $this->space->getTupleKey($tuple);
248255

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

327334
public function removeEntity(Entity $instance): self
328335
{
329-
$key = $this->space->getInstanceKey($instance);
336+
$key = (string) $this->space->getInstanceKey($instance);
330337

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

373380
public function save(Entity $instance): Entity
374381
{
375-
$key = $this->space->getInstanceKey($instance);
382+
$key = (string) $this->space->getInstanceKey($instance);
376383
$client = $this->getMapper()->getClient();
377384

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

479486
public function getOriginal(Entity $instance): ?array
480487
{
481-
$key = $this->space->getInstanceKey($instance);
488+
$key = (string) $this->space->getInstanceKey($instance);
482489

483490
if (array_key_exists($key, $this->original)) {
484491
return $this->original[$key];

tests/UuidTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,31 @@
1515

1616
class UuidTest extends TestCase
1717
{
18-
public function test()
18+
public function testPrimaryKey()
19+
{
20+
$mapper = $this->createMapper();
21+
$this->clean($mapper);
22+
23+
$mapper->getSchema()->createSpace('test_space', [
24+
'is_sync' => true,
25+
'engine' => 'memtx',
26+
'properties' => [
27+
'id' => 'uuid',
28+
],
29+
])
30+
->addIndex([ 'fields' => 'id' ]);
31+
32+
$uuid = Uuid::v4();
33+
34+
$instance = $mapper->create('test_space', [
35+
'id' => $uuid,
36+
]);
37+
38+
$mapper->getRepository('test_space')->forget($instance);
39+
$this->assertNotNull($mapper->findOne('test_space', $uuid));
40+
}
41+
42+
public function testBasics()
1943
{
2044
$mapper = $this->createMapper();
2145
$this->clean($mapper);

0 commit comments

Comments
 (0)