|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiAPI Yii2 base project for building API |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hiapi |
|
6
|
|
|
* @package hiapi |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiapi\repositories; |
|
12
|
|
|
|
|
13
|
|
|
use hiapi\components\ConnectionInterface; |
|
14
|
|
|
use hiapi\query\Specification; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
|
|
17
|
|
|
abstract class BaseRepository extends \yii\base\Component |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConnectionInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $db; |
|
23
|
|
|
|
|
24
|
|
|
protected $factory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
public $queryClass; |
|
30
|
|
|
|
|
31
|
|
|
public function find(ActiveQuery $query) |
|
32
|
|
|
{ |
|
33
|
|
|
$query->setRepository($this); |
|
34
|
|
|
|
|
35
|
|
|
return $query; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function setRecordClass($value) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->recordClass = $value; |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getRecordClass() |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->recordClass === null) { |
|
|
|
|
|
|
46
|
|
|
$this->recordClass = $this->findRecordClass(); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->recordClass; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function findRecordClass() |
|
53
|
|
|
{ |
|
54
|
|
|
$parts = explode('\\', get_called_class()); |
|
55
|
|
|
|
|
56
|
|
|
return implode('\\', $parts); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
public function findAll(Specification $specification) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$query = $this->buildSelectQuery($specification); |
|
62
|
|
|
$rows = $query->createCommand($this->db)->queryAll(); |
|
|
|
|
|
|
63
|
|
|
$rows = array_map(function ($row) use ($query) { |
|
64
|
|
|
return $query->restoreHierarchy($row); |
|
65
|
|
|
}, $rows); |
|
66
|
|
|
|
|
67
|
|
|
return $this->createMultiple($rows); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function findOne(Specification $specification) |
|
71
|
|
|
{ |
|
72
|
|
|
$rows = $this->findAll($specification->limit(1)); |
|
73
|
|
|
|
|
74
|
|
|
return reset($rows); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
public function old_findOne(Specification $specification) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$specification->limit(1); |
|
80
|
|
|
$query = $this->buildSelectQuery($specification); |
|
81
|
|
|
$row = $query->createCommand($this->db)->queryOne(); |
|
|
|
|
|
|
82
|
|
|
$row = $query->restoreHierarchy($row); |
|
83
|
|
|
|
|
84
|
|
|
return $this->create($row); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function buildSelectQuery(Specification $specification) |
|
88
|
|
|
{ |
|
89
|
|
|
return $specification->applyTo($this->buildQuery()->initSelect()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function buildQuery() |
|
93
|
|
|
{ |
|
94
|
|
|
return Yii::createObject($this->getQueryClass()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function getQueryClass() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->queryClass; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function createMultiple($rows) |
|
103
|
|
|
{ |
|
104
|
|
|
$entities = []; |
|
105
|
|
|
foreach ($rows as $row) { |
|
106
|
|
|
$entities[] = $this->create($row); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $entities; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function create(array $row) |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->factory->create($this->createDto($row)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function createDto(array $row) |
|
118
|
|
|
{ |
|
119
|
|
|
$class = $this->getEntityCreationDtoClass(); |
|
120
|
|
|
$dto = new $class(); |
|
121
|
|
|
$props = array_keys(get_object_vars($dto)); |
|
122
|
|
|
|
|
123
|
|
|
foreach ($props as $name) { |
|
124
|
|
|
if (isset($row[$name])) { |
|
125
|
|
|
$dto->$name = $row[$name]; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $dto; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
protected function getEntityCreationDtoClass() |
|
133
|
|
|
{ |
|
134
|
|
|
$class = new \ReflectionClass($this->factory); |
|
135
|
|
|
$method = $class->getMethod('create'); |
|
136
|
|
|
$arg = reset($method->getParameters()); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
return $arg->getClass()->getName(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function createEntity($entityClass, $row) |
|
142
|
|
|
{ |
|
143
|
|
|
return Yii::$app->entityManager->getRepository($entityClass)->create($row); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.