1 | <?php |
||
26 | trait UserTrait |
||
27 | { |
||
28 | use PasswordTrait, |
||
29 | RegistrationTrait, |
||
30 | IdentityTrait; |
||
31 | |||
32 | /** |
||
33 | * Create new entity model associated with current user. The model to be created |
||
34 | * must be extended from [[BaseBlameableModel]], [[BaseMongoBlameableModel]], |
||
35 | * [[BaseRedisBlameableModel]], or any other classes used [[BlameableTrait]]. |
||
36 | * if $config does not specify `userClass` property, self will be assigned to. |
||
37 | * @param string $className Full qualified class name. |
||
38 | * @param array $config name-value pairs that will be used to initialize |
||
39 | * the object properties. |
||
40 | * @param boolean $loadDefault Determines whether loading default values |
||
41 | * after entity model created. |
||
42 | * Notice! The [[\yii\mongodb\ActiveRecord]] and [[\yii\redis\ActiveRecord]] |
||
43 | * does not support loading default value. If you want to assign properties |
||
44 | * with default values, please define the `default` rule(s) for properties in |
||
45 | * `rules()` method and return them by yourself if you don't specified them in config param. |
||
46 | * @param boolean $skipIfSet whether existing value should be preserved. |
||
47 | * This will only set defaults for attributes that are `null`. |
||
48 | * @return [[$className]] new model created with specified configuration. |
||
|
|||
49 | */ |
||
50 | 51 | public function create($className, $config = [], $loadDefault = true, $skipIfSet = true) |
|
51 | { |
||
52 | 51 | if (!isset($config['userClass'])) { |
|
53 | 51 | $config['userClass'] = static::class; |
|
54 | } |
||
55 | 51 | if (isset($config['class'])) { |
|
56 | 15 | unset($config['class']); |
|
57 | } |
||
58 | 51 | $entity = new $className($config); |
|
59 | 51 | $entity->setUser($this); |
|
60 | 51 | if ($loadDefault && method_exists($entity, 'loadDefaultValues')) { |
|
61 | 24 | $entity->loadDefaultValues($skipIfSet); |
|
62 | } |
||
63 | 51 | return $entity; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Find existed, or create new model. |
||
68 | * If model to be found doesn't exist, and $config is null, the parameter |
||
69 | * `$condition` will be regarded as properties of new model. |
||
70 | * If you want to know whether the returned model is new model, please check |
||
71 | * the return value of `getIsNewRecord()` method. |
||
72 | * @param string $className Full qualified class name. |
||
73 | * @param array $condition Search condition, or properties if not found and |
||
74 | * $config is null. |
||
75 | * @param array $config new model's configuration array. If you specify this |
||
76 | * parameter, the $condition will be skipped when created one. |
||
77 | * @return [[$className]] the existed model, or new model created by specified |
||
78 | * condition or configuration. |
||
79 | */ |
||
80 | 1 | public function findOneOrCreate($className, $condition = [], $config = null) |
|
81 | { |
||
82 | 1 | $entity = new $className(['skipInit' => true]); |
|
83 | 1 | if (!isset($condition[$entity->createdByAttribute])) { |
|
84 | 1 | $condition[$entity->createdByAttribute] = $this->guid; |
|
85 | } |
||
86 | 1 | $model = $className::findOne($condition); |
|
87 | 1 | if (!$model) { |
|
88 | 1 | if ($config === null || !is_array($config)) { |
|
89 | 1 | $config = $condition; |
|
90 | } |
||
91 | 1 | $model = $this->create($className, $config); |
|
92 | } |
||
93 | 1 | return $model; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * This method is only used for overriding [[removeSelf()]] in [[TimestampTrait]]. |
||
98 | * @see deregister() |
||
99 | * @return boolean |
||
100 | */ |
||
101 | 4 | public function removeSelf() |
|
105 | |||
106 | /** |
||
107 | * Get all rules with current user properties. |
||
108 | * @return array all rules. |
||
109 | */ |
||
110 | 136 | public function rules() |
|
111 | { |
||
112 | 136 | return array_merge( |
|
113 | 136 | parent::rules(), |
|
114 | 136 | $this->getPasswordHashRules(), |
|
115 | 136 | $this->getPasswordResetTokenRules(), |
|
116 | 136 | $this->getSourceRules(), |
|
117 | 136 | $this->getStatusRules(), |
|
118 | 136 | $this->getAuthKeyRules(), |
|
119 | 136 | $this->getAccessTokenRules() |
|
120 | ); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @var string[] Subsidiary map. |
||
125 | * Array key represents class alias, |
||
126 | * array value represents the full qualified class name corresponds to the alias. |
||
127 | * |
||
128 | * For example: |
||
129 | * ```php |
||
130 | * public $subsidiaryMap = [ |
||
131 | * 'Profile' => 'app\models\user\Profile', |
||
132 | * ]; |
||
133 | * ``` |
||
134 | * or: |
||
135 | * ```php |
||
136 | * public $subsidiaryMap = [ |
||
137 | * 'Profile' => [ |
||
138 | * 'class' => 'app\models\user\Profile', |
||
139 | * 'max' => 1, |
||
140 | * ] |
||
141 | * ]; |
||
142 | * |
||
143 | * If you want to create subsidiary model and the class is not found, the array elements will be taken. |
||
144 | * @see normalizeSubsidiaryClass |
||
145 | */ |
||
146 | public $subsidiaryMap = []; |
||
147 | |||
148 | /** |
||
149 | * |
||
150 | * @param string $class |
||
151 | * @return integer|null |
||
152 | */ |
||
153 | public function getSubsidiaryInstanceMax($class) |
||
160 | |||
161 | /** |
||
162 | * |
||
163 | * @param type $class |
||
164 | * @param type $config |
||
165 | * @return type |
||
166 | * @todo 区分字符串和类的实例两种情况。 |
||
167 | */ |
||
168 | public function createSubsidiary($class, $config = []) |
||
169 | { |
||
170 | if (!is_string($class) || empty($class)) { |
||
171 | return null; |
||
172 | } |
||
173 | $className = ''; |
||
174 | if (class_exists($class)) { |
||
175 | $className = $class; |
||
176 | } elseif (array_key_exists($class, $this->subsidiaryMap)) { |
||
177 | if (class_exists($this->subsidiaryMap[$class])) { |
||
178 | $className = $this->subsidiaryMap[$class]; |
||
179 | } elseif (class_exists($this->subsidiaryMap[$class]['class'])) { |
||
180 | $className = $this->subsidiaryMap[$class]['class']; |
||
181 | } |
||
182 | } else { |
||
183 | return null; |
||
184 | } |
||
185 | return $this->create($className, $config); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * |
||
190 | * @param string $name |
||
191 | * @param array $params |
||
192 | * @return type |
||
193 | */ |
||
194 | public function __call($name, $params) |
||
203 | } |
||
204 |
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.