Skip to content

Commit b477a3a

Browse files
author
Composite PHP
committed
Move raw selects to separate trait and build entities by default
1 parent 019af83 commit b477a3a

17 files changed

+237
-192
lines changed

doc/cache.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ class PostsTable extends AbstractCachedTable
4646

4747
public function findByPk(int $id): ?Post
4848
{
49-
return $this->createEntity($this->_findByPkCached($id));
49+
return $this->_findByPkCached($id);
5050
}
5151

5252
/**
5353
* @return Post[]
5454
*/
5555
public function findAllFeatured(): array
5656
{
57-
return $this->createEntities($this->_findAll(
58-
['is_featured' => true],
59-
));
57+
return $this->_findAll(['is_featured' => true]);
6058
}
6159

6260
public function countAllFeatured(): int

doc/example.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@ class UsersTable extends \Composite\DB\AbstractTable
4343

4444
public function findByPk(int $id): ?User
4545
{
46-
return $this->createEntity($this->_findByPk($id));
46+
return $this->_findByPk($id);
4747
}
4848

4949
/**
5050
* @return User[]
5151
*/
5252
public function findAllActive(): array
5353
{
54-
return $this->createEntities($this->_findAll(
55-
['status' => Status::ACTIVE],
56-
));
54+
return $this->_findAll(['status' => Status::ACTIVE]);
5755
}
5856

5957
public function countAllActive(): int

doc/table.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ class UsersTable extends AbstractTable
3838

3939
public function findOne(int $id): ?User
4040
{
41-
return $this->createEntity($this->_findOne($id));
41+
return $this->_findByPk($id);
4242
}
4343

4444
/**
4545
* @return User[]
4646
*/
4747
public function findAll(): array
4848
{
49-
return $this->createEntities($this->_findAll());
49+
return $this->_findAll();
5050
}
5151

5252
public function countAll(): int
@@ -67,13 +67,12 @@ Example with internal helper:
6767
*/
6868
public function findAllActiveAdults(): array
6969
{
70-
$rows = $this->_findAll(
70+
return $this->_findAll(
7171
new Where(
7272
'age > :age AND status = :status',
7373
['age' => 18, 'status' => Status::ACTIVE->name],
7474
)
7575
);
76-
return $this->createEntities($rows);
7776
}
7877
```
7978

@@ -84,11 +83,10 @@ Or it might be simplified to:
8483
*/
8584
public function findAllActiveAdults(): array
8685
{
87-
$rows = $this->_findAll([
86+
return $this->_findAll([
8887
'age' => ['>', 18],
8988
'status' => Status:ACTIVE,
9089
]);
91-
return $this->createEntities($rows);
9290
}
9391
```
9492

src/AbstractCachedTable.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Composite\DB;
44

5-
use Composite\DB\Exceptions\DbException;
6-
use Composite\DB\Tests\TestStand\Entities\TestAutoincrementEntity;
75
use Composite\Entity\AbstractEntity;
86
use Psr\SimpleCache\CacheInterface;
97
use Ramsey\Uuid\UuidInterface;
108

119
abstract class AbstractCachedTable extends AbstractTable
1210
{
11+
use SelectRawTrait;
12+
1313
protected const CACHE_VERSION = 1;
1414

1515
public function __construct(
@@ -94,44 +94,47 @@ private function collectCacheKeysByEntity(AbstractEntity $entity): array
9494
}
9595

9696
/**
97-
* @return array<string, mixed>|null
97+
* @return AbstractEntity|null
9898
*/
99-
protected function _findByPkCached(mixed $pk, null|int|\DateInterval $ttl = null): ?array
99+
protected function _findByPkCached(mixed $pk, null|int|\DateInterval $ttl = null): mixed
100100
{
101101
return $this->_findOneCached($this->getPkCondition($pk), $ttl);
102102
}
103103

104104
/**
105-
* @param array<string, mixed> $condition
105+
* @param array<string, mixed> $where
106106
* @param int|\DateInterval|null $ttl
107-
* @return array<string, mixed>|null
107+
* @return AbstractEntity|null
108108
*/
109-
protected function _findOneCached(array $condition, null|int|\DateInterval $ttl = null): ?array
109+
protected function _findOneCached(array $where, null|int|\DateInterval $ttl = null): mixed
110110
{
111-
return $this->getCached(
112-
$this->getOneCacheKey($condition),
113-
fn() => $this->_findOne($condition),
111+
$row = $this->getCached(
112+
$this->getOneCacheKey($where),
113+
fn() => $this->_findOneRaw($where),
114114
$ttl,
115-
) ?: null;
115+
);
116+
return $this->createEntity($row);
116117
}
117118

118119
/**
119120
* @param array<string, mixed>|Where $where
120121
* @param array<string, string>|string $orderBy
121-
* @return array<string, mixed>[]
122+
* @return array<AbstractEntity>|array<array-key, AbstractEntity>
122123
*/
123124
protected function _findAllCached(
124125
array|Where $where = [],
125126
array|string $orderBy = [],
126127
?int $limit = null,
127128
null|int|\DateInterval $ttl = null,
129+
?string $keyColumnName = null,
128130
): array
129131
{
130-
return $this->getCached(
132+
$rows = $this->getCached(
131133
$this->getListCacheKey($where, $orderBy, $limit),
132-
fn() => $this->_findAll(where: $where, orderBy: $orderBy, limit: $limit),
134+
fn() => $this->_findAllRaw(where: $where, orderBy: $orderBy, limit: $limit),
133135
$ttl,
134136
);
137+
return $this->createEntities($rows, $keyColumnName);
135138
}
136139

137140
/**
@@ -165,10 +168,14 @@ protected function getCached(string $cacheKey, callable $dataCallback, null|int|
165168
/**
166169
* @param mixed[] $ids
167170
* @param int|\DateInterval|null $ttl
168-
* @return array<array<string, mixed>>
171+
* @return array<AbstractEntity>|array<array-key, AbstractEntity>
169172
* @throws \Psr\SimpleCache\InvalidArgumentException
170173
*/
171-
protected function _findMultiCached(array $ids, null|int|\DateInterval $ttl = null): array
174+
protected function _findMultiCached(
175+
array $ids,
176+
null|int|\DateInterval $ttl = null,
177+
?string $keyColumnName = null,
178+
): array
172179
{
173180
$result = $cacheKeys = $foundIds = [];
174181
foreach ($ids as $id) {
@@ -191,7 +198,7 @@ protected function _findMultiCached(array $ids, null|int|\DateInterval $ttl = nu
191198
$result[] = $row;
192199
}
193200
}
194-
return $result;
201+
return $this->createEntities($result, $keyColumnName);
195202
}
196203

197204
/**
@@ -271,7 +278,7 @@ protected function buildCacheKey(mixed ...$parts): string
271278

272279
private function formatStringForCacheKey(string $string): string
273280
{
274-
$string = mb_strtolower($string);
281+
$string = strtolower($string);
275282
$string = str_replace(['!=', '<>', '>', '<', '='], ['_not_', '_not_', '_gt_', '_lt_', '_eq_'], $string);
276283
$string = (string)preg_replace('/\W/', '_', $string);
277284
return trim((string)preg_replace('/_+/', '_', $string), '_');

0 commit comments

Comments
 (0)