|
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\query\Specification; |
|
14
|
|
|
use Yii; |
|
15
|
|
|
|
|
16
|
|
|
abstract class BaseRepository extends \yii\base\Component |
|
17
|
|
|
{ |
|
18
|
|
|
protected $db; |
|
19
|
|
|
|
|
20
|
|
|
protected $factory; |
|
21
|
|
|
|
|
22
|
|
|
public $queryClass; |
|
23
|
|
|
|
|
24
|
|
|
public function find(ActiveQuery $query) |
|
25
|
|
|
{ |
|
26
|
|
|
$query->setRepository($this); |
|
27
|
|
|
|
|
28
|
|
|
return $query; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected $_recordClass; |
|
32
|
|
|
|
|
33
|
|
|
public function setRecordClass($value) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->recordClass = $value; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getRecordClass() |
|
39
|
|
|
{ |
|
40
|
|
|
if ($this->recordClass === null) { |
|
|
|
|
|
|
41
|
|
|
$this->recordClass = $this->findRecordClass(); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $this->recordClass; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function findRecordClass() |
|
48
|
|
|
{ |
|
49
|
|
|
$parts = explode('\\', get_called_class()); |
|
50
|
|
|
|
|
51
|
|
|
return implode('\\', $parts); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function findAll(Specification $specification) |
|
55
|
|
|
{ |
|
56
|
|
|
$query = $this->buildSelectQuery($specification); |
|
57
|
|
|
$rows = $query->createCommand($this->db)->queryAll(); |
|
58
|
|
|
/// todo $this->addWithes(); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
return $this->createMultiple($rows); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function findOne(Specification $specification) |
|
64
|
|
|
{ |
|
65
|
|
|
$specification->limit(1); |
|
66
|
|
|
$query = $this->buildSelectQuery($specification); |
|
67
|
|
|
$row = $query->createCommand($this->db)->queryOne(); |
|
68
|
|
|
|
|
69
|
|
|
return $this->create($row); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function buildSelectQuery(Specification $specification) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->buildQuery()->initSelect()->apply($specification); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function buildQuery() |
|
78
|
|
|
{ |
|
79
|
|
|
return Yii::createObject($this->getQueryClass()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function getQueryClass() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->queryClass; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function createMultiple($rows) |
|
88
|
|
|
{ |
|
89
|
|
|
$entities = []; |
|
90
|
|
|
foreach ($rows as $row) { |
|
91
|
|
|
$entities[] = $this->create($row); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $entities; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function create(array $row) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->factory->create($this->createDto($row)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function createDto(array $row) |
|
103
|
|
|
{ |
|
104
|
|
|
$class = $this->getEntityCreationDtoClass(); |
|
105
|
|
|
$dto = new $class(); |
|
106
|
|
|
$props = array_keys(get_object_vars($dto)); |
|
107
|
|
|
foreach ($props as $name) { |
|
108
|
|
|
if (isset($row[$name])) { |
|
109
|
|
|
$dto->{$name} = $row[$name]; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $dto; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function getEntityCreationDtoClass() |
|
117
|
|
|
{ |
|
118
|
|
|
$class = new \ReflectionClass($this->factory); |
|
119
|
|
|
$method = $class->getMethod('create'); |
|
120
|
|
|
$arg = reset($method->getParameters()); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
return $arg->getClass()->getName(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.