1 | <?php |
||
21 | trait SubsidiaryTrait |
||
22 | { |
||
23 | /** |
||
24 | * @var string[] Subsidiary map. |
||
25 | * Array key represents class alias, |
||
26 | * array value represents the full qualified class name corresponds to the alias. |
||
27 | * |
||
28 | * For example: |
||
29 | ```php |
||
30 | public $subsidiaryMap = [ |
||
31 | 'Profile' => 'app\models\user\Profile', |
||
32 | ]; |
||
33 | ``` |
||
34 | * Or |
||
35 | ```php |
||
36 | public $subsidiaryMap = [ |
||
37 | 'Profile' => [ |
||
38 | 'class' => 'app\models\user\Profile', |
||
39 | ], |
||
40 | ]; |
||
41 | ``` |
||
42 | * |
||
43 | * The other elements will be taken if subsidiary configuration does not specify. |
||
44 | * If you want to create subsidiary model and the class is not found, the array elements will be taken. |
||
45 | */ |
||
46 | public $subsidiaryMap = []; |
||
47 | |||
48 | /** |
||
49 | * Add subsidiary class to map. |
||
50 | * @param string $name |
||
51 | * @param string|array $config |
||
52 | * @return boolean |
||
53 | * @throws InvalidConfigException |
||
54 | */ |
||
55 | 7 | public function addSubsidiaryClass($name, $config) |
|
56 | { |
||
57 | 7 | if (!is_string($name) || empty($name)) { |
|
58 | 2 | throw new InvalidConfigException('Subsidiary name not specified.'); |
|
59 | } |
||
60 | 7 | $name = strtolower($name); |
|
61 | 7 | if (!is_array($config)) { |
|
62 | 7 | if (is_string($config) && !empty($config)) { |
|
63 | 7 | $this->subsidiaryMap[$name] = ['class' => $config]; |
|
64 | } else { |
||
65 | throw new InvalidConfigException('Subsidiary class not specified.'); |
||
66 | } |
||
67 | } else { |
||
68 | 2 | if (isset($config['class']) && class_exists($config['class'])) { |
|
69 | 2 | $this->subsidiaryMap[$name] = $config; |
|
70 | } else { |
||
71 | 1 | throw new InvalidConfigException('Subsidiary class not specified.'); |
|
72 | } |
||
73 | } |
||
74 | 7 | return true; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Remove subsidiary. |
||
79 | * @param string $name |
||
80 | * @return boolean |
||
81 | */ |
||
82 | 1 | public function removeSubsidiary($name) |
|
83 | { |
||
84 | 1 | $name = strtolower($name); |
|
85 | 1 | if (array_key_exists($name, $this->subsidiaryMap)) { |
|
86 | 1 | unset($this->subsidiaryMap[$name]); |
|
87 | 1 | return true; |
|
88 | } |
||
89 | 1 | return false; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Get subsidiary class. |
||
94 | * @param string $name |
||
95 | * @return string |
||
96 | */ |
||
97 | 7 | public function getSubsidiaryClass($name) |
|
98 | { |
||
99 | 7 | $name = strtolower($name); |
|
100 | 7 | if (array_key_exists($name, $this->subsidiaryMap) && array_key_exists('class', $this->subsidiaryMap[$name])) { |
|
101 | 7 | return class_exists($this->subsidiaryMap[$name]['class']) ? $this->subsidiaryMap[$name]['class'] : null; |
|
102 | } |
||
103 | 1 | return null; |
|
104 | } |
||
105 | |||
106 | 2 | public function getSubsidiaries($name, $limit = 'all', $page = 0) |
|
118 | |||
119 | 7 | public function __call($name, $arguments) |
|
128 | |||
129 | /** |
||
130 | * Find existed, or create new model. |
||
131 | * If model to be found doesn't exist, and $config is null, the parameter |
||
132 | * `$condition` will be regarded as properties of new model. |
||
133 | * If you want to know whether the returned model is new model, please check |
||
134 | * the return value of `getIsNewRecord()` method. |
||
135 | * @param string $className Full qualified class name. |
||
136 | * @param array $condition Search condition, or properties if not found and |
||
137 | * $config is null. |
||
138 | * @param array $config new model's configuration array. If you specify this |
||
139 | * parameter, the $condition will be skipped when created one. |
||
140 | * @return [[$className]] the existed model, or new model created by specified |
||
|
|||
141 | * condition or configuration. |
||
142 | */ |
||
143 | 1 | public function findOneOrCreate($className, $condition = [], $config = null) |
|
158 | |||
159 | /** |
||
160 | * Create new entity model associated with current user. The model to be created |
||
161 | * must be extended from [[BaseBlameableModel]], [[BaseMongoBlameableModel]], |
||
162 | * [[BaseRedisBlameableModel]], or any other classes used [[BlameableTrait]]. |
||
163 | * if $config does not specify `hostClass` property, self will be assigned to. |
||
164 | * @param string $className Full qualified class name. |
||
165 | * @param array $config name-value pairs that will be used to initialize |
||
166 | * the object properties. |
||
167 | * @param boolean $loadDefault Determines whether loading default values |
||
168 | * after entity model created. |
||
169 | * Notice! The [[\yii\mongodb\ActiveRecord]] and [[\yii\redis\ActiveRecord]] |
||
170 | * does not support loading default value. If you want to assign properties |
||
171 | * with default values, please define the `default` rule(s) for properties in |
||
172 | * `rules()` method and return them by yourself if you don't specified them in config param. |
||
173 | * @param boolean $skipIfSet whether existing value should be preserved. |
||
174 | * This will only set defaults for attributes that are `null`. |
||
175 | * @return [[$className]] new model created with specified configuration. |
||
176 | */ |
||
177 | 168 | public function create($className, $config = [], $loadDefault = true, $skipIfSet = true) |
|
192 | |||
193 | /** |
||
194 | * |
||
195 | * @param string $name |
||
196 | * @param array $config |
||
197 | * @return type |
||
198 | */ |
||
199 | 7 | public function createSubsidiary($name, $config) |
|
214 | } |
||
215 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.