1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chubbyphp\Model; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
7
|
|
|
|
8
|
|
|
abstract class AbstractDoctrineRepository implements RepositoryInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Connection |
12
|
|
|
*/ |
13
|
|
|
private $connection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Connection $connection |
17
|
|
|
*/ |
18
|
|
|
public function __construct(Connection $connection) |
19
|
|
|
{ |
20
|
|
|
$this->connection = $connection; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $id |
25
|
|
|
* |
26
|
|
|
* @return ModelInterface|null |
27
|
|
|
*/ |
28
|
|
|
public function find(string $id) |
29
|
|
|
{ |
30
|
|
|
$qb = $this->connection->createQueryBuilder(); |
31
|
|
|
$qb->select('*')->from($this->getTable())->where($qb->expr()->eq('id', ':id'))->setParameter('id', $id); |
32
|
|
|
|
33
|
|
|
$row = $qb->execute()->fetch(\PDO::FETCH_ASSOC); |
34
|
|
|
if (false === $row) { |
35
|
|
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @var ModelInterface $modelClass */ |
39
|
|
|
$modelClass = $this->getModelClass(); |
40
|
|
|
|
41
|
|
|
return $modelClass::fromRow($row); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $criteria |
46
|
|
|
* |
47
|
|
|
* @return null|ModelInterface |
48
|
|
|
*/ |
49
|
|
|
public function findOneBy(array $criteria = []) |
50
|
|
|
{ |
51
|
|
|
$qb = $this->getFindByQueryBuilder($criteria)->setMaxResults(1); |
52
|
|
|
|
53
|
|
|
$row = $qb->execute()->fetch(\PDO::FETCH_ASSOC); |
54
|
|
|
if (false === $row) { |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @var ModelInterface $modelClass */ |
59
|
|
|
$modelClass = $this->getModelClass(); |
60
|
|
|
|
61
|
|
|
return $modelClass::fromRow($row); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $criteria |
66
|
|
|
* |
67
|
|
|
* @return ModelInterface[]|array |
68
|
|
|
*/ |
69
|
|
|
public function findBy(array $criteria = []): array |
70
|
|
|
{ |
71
|
|
|
$rows = $this->getFindByQueryBuilder($criteria)->execute()->fetchAll(\PDO::FETCH_ASSOC); |
72
|
|
|
|
73
|
|
|
if ([] === $rows) { |
74
|
|
|
return []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @var ModelInterface $modelClass */ |
78
|
|
|
$modelClass = $this->getModelClass(); |
79
|
|
|
|
80
|
|
|
$models = []; |
81
|
|
|
foreach ($rows as $row) { |
82
|
|
|
$models[] = $modelClass::fromRow($row); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $models; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $criteria |
90
|
|
|
* |
91
|
|
|
* @return QueryBuilder |
92
|
|
|
*/ |
93
|
|
|
private function getFindByQueryBuilder(array $criteria = []): QueryBuilder |
94
|
|
|
{ |
95
|
|
|
$qb = $this->connection->createQueryBuilder(); |
96
|
|
|
$qb->select('*')->from($this->getTable()); |
97
|
|
|
|
98
|
|
|
foreach ($criteria as $field => $value) { |
99
|
|
|
$qb->andWhere($qb->expr()->eq($field, ':'.$field)); |
100
|
|
|
$qb->setParameter($field, $value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $qb; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param ModelInterface $model |
108
|
|
|
*/ |
109
|
|
|
public function insert(ModelInterface $model) |
110
|
|
|
{ |
111
|
|
|
$this->connection->insert($this->getTable(), $model->toRow()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param ModelInterface $model |
116
|
|
|
*/ |
117
|
|
|
public function update(ModelInterface $model) |
118
|
|
|
{ |
119
|
|
|
$this->connection->update($this->getTable(), $model->toRow(), ['id' => $model->getId()]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param ModelInterface $model |
124
|
|
|
*/ |
125
|
|
|
public function delete(ModelInterface $model) |
126
|
|
|
{ |
127
|
|
|
$this->connection->delete($this->getTable(), ['id' => $model->getId()]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
abstract protected function getTable(): string; |
134
|
|
|
} |
135
|
|
|
|