Skip to content

Commit 871ba52

Browse files
authored
Update records (#6)
Update records
2 parents 3bba447 + f6ecaa9 commit 871ba52

File tree

6 files changed

+64
-1
lines changed

6 files changed

+64
-1
lines changed

etc/db/seeds/BlogPostsSeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function run(): void
2020
'publisher_id' => '15f25357-4b3d-4d4d-b6a5-2ceb93864b77',
2121
'title' => 'Moar Cats!',
2222
'contents' => 'qlqweofu b02qw yu9 dqiwuhe uofq2hep fuoq2pho fp2uhu pu2p 2qpoh weh uwqhfu wqif',
23-
'views' => 133,
23+
'views' => 166,
2424
],
2525
];
2626

src/Hydrator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GeneratedHydrator\Configuration;
66
use WyriHaximus\React\SimpleORM\Entity\Field;
7+
use Zend\Hydrator\HydratorInterface;
78

89
final class Hydrator
910
{
@@ -48,6 +49,15 @@ public function hydrate(InspectedEntity $inspectedEntity, array $data): EntityIn
4849
return $this->hydrators[$class]->hydrate($data, new $class());
4950
}
5051

52+
public function extract(InspectedEntity $inspectedEntity, EntityInterface $entity): array
53+
{
54+
$class = $inspectedEntity->getClass();
55+
/** @var HydratorInterface $hydrator */
56+
$hydrator = $this->hydrators[$class];
57+
58+
return $hydrator->extract($entity);
59+
}
60+
5161
/**
5262
* @param Field $field
5363
* @param mixed $value

src/Repository.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,29 @@ public function create(array $fields): PromiseInterface
7878
});
7979
}
8080

81+
public function update(EntityInterface $entity): PromiseInterface
82+
{
83+
$fields = $this->hydrator->extract($this->entity, $entity);
84+
foreach ($fields as $key => $value) {
85+
if (\is_scalar($value)) {
86+
continue;
87+
}
88+
89+
unset($fields[$key]);
90+
}
91+
92+
return $this->client->query(
93+
QueryBuilder::create()->
94+
update($fields)->
95+
into($this->entity->getTable())->
96+
where('id', '=', $entity->getId())
97+
)->toPromise()->then(function () use ($entity) {
98+
return $this->fetch([
99+
['id', '=', $entity->getId()],
100+
])->take(1)->toPromise();
101+
});
102+
}
103+
81104
private function buildSelectQuery(array $where = [], array $order = []): QueryBuilder
82105
{
83106
$query = $this->getBaseQuery();

src/RepositoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ public function page(int $page, array $where = [], array $order = [], int $perPa
1616
public function fetch(array $where = [], array $order = []): Observable;
1717

1818
public function create(array $fields): PromiseInterface;
19+
20+
public function update(EntityInterface $entity): PromiseInterface;
1921
}

tests/FunctionalTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,24 @@ public function createUser(): void
245245

246246
self::assertSame($name, $user->getName());
247247
}
248+
249+
/**
250+
* @test
251+
*/
252+
public function increaseViews(): void
253+
{
254+
$repository = $this->client->getRepository(BlogPostStub::class);
255+
256+
self::assertSame(
257+
167,
258+
$this->await(
259+
$repository->fetch()->takeLast(1)->toPromise()->then(function (BlogPostStub $blogPost) {
260+
return $blogPost->withViews($blogPost->getViews() + 1);
261+
})->then(function (BlogPostStub $blogPost) use ($repository) {
262+
return $repository->update($blogPost);
263+
}),
264+
$this->loop
265+
)->getViews()
266+
);
267+
}
248268
}

tests/Stub/BlogPostStub.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,12 @@ public function getViews(): int
9393
{
9494
return $this->views;
9595
}
96+
97+
public function withViews(int $views): self
98+
{
99+
$clone = clone $this;
100+
$clone->views = $views;
101+
102+
return $clone;
103+
}
96104
}

0 commit comments

Comments
 (0)