|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\ActiveRecord; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\ActiveRecord\Redis\ActiveQuery as RedisActiveQuery; |
|
8
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
|
9
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
|
10
|
|
|
use Yiisoft\Factory\Factory; |
|
11
|
|
|
|
|
12
|
|
|
final class ActiveRecordFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Factory |
|
16
|
|
|
*/ |
|
17
|
|
|
private Factory $factory; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Factory $factory) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->factory = $factory; |
|
22
|
|
|
} |
|
23
|
2 |
|
|
|
24
|
|
|
/** |
|
25
|
2 |
|
* Allows you to create an active record instance through the factory. |
|
26
|
|
|
* |
|
27
|
2 |
|
* @param string $arClass active record class. |
|
28
|
|
|
* @param ConnectionInterface|null $db the database connection used for creating active record instances. |
|
29
|
|
|
* |
|
30
|
|
|
* @throws InvalidConfigException |
|
31
|
|
|
* |
|
32
|
|
|
* @return ActiveRecordInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
public function createAR(string $arClass, ConnectionInterface $db = null): ActiveRecordInterface |
|
35
|
|
|
{ |
|
36
|
|
|
$params = [ |
|
37
|
|
|
'class' => $arClass, |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
if ($db) { |
|
41
|
|
|
$params['__construct()']['db'] = $db; |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
return $this->factory->create($params); |
|
45
|
|
|
} |
|
46
|
2 |
|
|
|
47
|
|
|
/** |
|
48
|
2 |
|
* Allows you to create an active query instance through the factory. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $arClass active record class. |
|
51
|
|
|
* @param string|null $queryClass custom query active query class. |
|
52
|
|
|
* @param ConnectionInterface $connection the database connection used for creating active query instances. |
|
53
|
|
|
* |
|
54
|
|
|
* @throws InvalidConfigException |
|
55
|
|
|
* |
|
56
|
|
|
* @return ActiveQueryInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
public function createQueryTo( |
|
59
|
|
|
string $arClass, |
|
60
|
|
|
string $queryClass = null, |
|
61
|
|
|
ConnectionInterface $db = null |
|
62
|
|
|
): ActiveQueryInterface { |
|
63
|
|
|
$params = [ |
|
64
|
1 |
|
'class' => $queryClass ?? ActiveQuery::class, |
|
65
|
|
|
'__construct()' => [ |
|
66
|
1 |
|
'modelClass' => $arClass, |
|
67
|
|
|
], |
|
68
|
1 |
|
]; |
|
69
|
|
|
|
|
70
|
1 |
|
if ($db) { |
|
71
|
|
|
$params['__construct()']['db'] = $db; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->factory->create($params); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Allows you to create an redis active query instance through the factory. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $arClass active record class. |
|
81
|
|
|
* @param string|null $queryClass custom query active query class. |
|
82
|
|
|
* @param ConnectionInterface $connection the database connection used for creating active query instances. |
|
83
|
|
|
* |
|
84
|
2 |
|
* @throws InvalidConfigException |
|
85
|
|
|
* |
|
86
|
2 |
|
* @return ActiveQueryInterface |
|
87
|
2 |
|
*/ |
|
88
|
|
|
public function createRedisQueryTo( |
|
89
|
|
|
string $arClass, |
|
90
|
|
|
string $queryClass = null, |
|
91
|
|
|
ConnectionInterface $db = null |
|
92
|
|
|
): ActiveQueryInterface { |
|
93
|
|
|
$params = [ |
|
94
|
|
|
'class' => $queryClass ?? RedisActiveQuery::class, |
|
95
|
|
|
'__construct()' => [ |
|
96
|
2 |
|
'modelClass' => $arClass, |
|
97
|
|
|
], |
|
98
|
2 |
|
]; |
|
99
|
|
|
|
|
100
|
|
|
if ($db) { |
|
101
|
|
|
$params['__construct()']['db'] = $db; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->factory->create($params); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|