Skip to content

Commit ee4b864

Browse files
committed
instantiate trigger fix
1 parent 63748ef commit ee4b864

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/Repository.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,17 @@ public function create($data): Entity
4848
}
4949
}
5050

51-
$instance = $this->createInstance();
52-
foreach ($data as $key => $value) {
53-
if ($value instanceof Entity) {
54-
$value = $value->getRepository()->getSpace()->getIndex(0)->getValue($value->toArray()) ?: null;
55-
}
56-
$instance->$key = $value;
57-
}
58-
59-
foreach ($this->getMapper()->getPlugins() as $plugin) {
60-
$plugin->afterInstantiate($instance, $this->space);
61-
}
51+
$instance = $this->createInstance([], $data);
6252

6353
return $instance;
6454
}
6555

66-
public function createInstance(array $tuple = []): Entity
56+
public function createInstance(array $tuple = [], array $data = []): Entity
6757
{
6858
$class = Entity::class;
69-
$data = $this->getSpace()->getMap($tuple);
59+
$map = $this->getSpace()->getMap($tuple);
7060
foreach ($this->getMapper()->getPlugins() as $plugin) {
71-
$entityClass = $plugin->getEntityClass($this->space, $data);
61+
$entityClass = $plugin->getEntityClass($this->space, $map);
7262
if (!$entityClass) {
7363
continue;
7464
}
@@ -80,6 +70,17 @@ public function createInstance(array $tuple = []): Entity
8070

8171
$instance = new $class($this, $tuple);
8272

73+
foreach ($data as $key => $value) {
74+
if ($value instanceof Entity) {
75+
$value = $value->getRepository()->getSpace()->getIndex(0)->getValue($value->toArray()) ?: null;
76+
}
77+
$instance->$key = $value;
78+
}
79+
80+
foreach ($this->getMapper()->getPlugins() as $plugin) {
81+
$plugin->afterInstantiate($instance, $this->space);
82+
}
83+
8384
return $instance;
8485
}
8586

tests/MapperTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,31 @@
99
use Tarantool\Client\Middleware\LoggingMiddleware;
1010
use Tarantool\Client\Request\InsertRequest;
1111
use Tarantool\Client\Schema\Operations;
12+
use Tarantool\Mapper\Entity;
1213
use Tarantool\Mapper\Mapper;
14+
use Tarantool\Mapper\Plugin;
1315
use Tarantool\Mapper\Plugin\Procedure;
1416
use Tarantool\Mapper\Plugin\Sequence;
1517
use Tarantool\Mapper\Procedure\FindOrCreate;
1618
use Tarantool\Mapper\Schema;
1719

1820
class MapperTest extends TestCase
1921
{
22+
public function testAfterInstantiateTrigger()
23+
{
24+
$mapper = $this->createMapper();
25+
$mapper->getPlugin(new class ($mapper) extends Plugin {
26+
public function afterInstantiate(Entity $instance): Entity
27+
{
28+
$instance->mySecretProperty = 'tester';
29+
return $instance;
30+
}
31+
});
32+
33+
$this->assertSame($mapper->findOrFail('_vspace')->mySecretProperty, 'tester');
34+
$this->assertSame($mapper->getRepository('_vspace')->create([])->mySecretProperty, 'tester');
35+
}
36+
2037
public function testEmptyStringPersistence()
2138
{
2239
$mapper = $this->createMapper();

0 commit comments

Comments
 (0)