@@ -146,11 +146,16 @@ protected function getOption(string $name, $default = null)
146146 return self ::$ weakMap [$ this ][$ name ] ?? $ default ;
147147 }
148148
149- protected function setWeakData ($ key , $ name , $ value )
149+ private function setWeakData ($ key , $ name , $ value )
150150 {
151151 self ::$ weakMap [$ this ][$ key ][$ name ] = $ value ;
152152 }
153153
154+ private function getWeakData ($ key , $ name , $ default = null )
155+ {
156+ return self ::$ weakMap [$ this ][$ key ][$ name ] ?? $ default ;
157+ }
158+
154159 /**
155160 * 获取对应表名(仅限简单模型).
156161 *
@@ -213,7 +218,7 @@ private function getSimpleModel()
213218 {
214219 return Db::newQuery ()
215220 ->name ($ this ->getTableName ())
216- ->pk (self :: $ weakMap [ $ this ][ 'pk ' ] );
221+ ->pk ($ this -> getOption ( 'pk ' ) );
217222 }
218223
219224 /**
@@ -225,7 +230,8 @@ private function getSimpleModel()
225230 */
226231 protected function getFields (?string $ field = null )
227232 {
228- if (empty (self ::$ weakMap [$ this ]['schema ' ])) {
233+ $ schema = $ this ->getOption ('schema ' );
234+ if (empty ($ schema )) {
229235 if ($ this ->isView () || $ this ->isVirtual ()) {
230236 $ schema = $ this ->getOption ('type ' , []);
231237 } else {
@@ -236,8 +242,6 @@ protected function getFields(?string $field = null)
236242 }
237243
238244 $ this ->setOption ('schema ' , $ schema );
239- } else {
240- $ schema = $ this ->getOption ('schema ' );
241245 }
242246
243247 if ($ field ) {
@@ -282,7 +286,7 @@ protected function parseValidate(): string
282286 */
283287 public function model ()
284288 {
285- return self :: $ weakMap [ $ this ][ 'model ' ] ->schema (self :: $ weakMap [ $ this ][ 'schema ' ] );
289+ return $ this -> getOption ( 'model ' ) ->schema ($ this -> getOption ( 'schema ' ) );
286290 }
287291
288292 /**
@@ -303,7 +307,7 @@ protected function initializeData(array | object $data, bool $fromSave = false)
303307
304308 // 实体模型赋值
305309 foreach ($ data as $ name => $ val ) {
306- if (in_array ($ name , self :: $ weakMap [ $ this ][ 'disuse ' ] )) {
310+ if (in_array ($ name , $ this -> getOption ( 'disuse ' ) )) {
307311 // 废弃字段
308312 continue ;
309313 }
@@ -360,7 +364,7 @@ protected function parseRelationData(array $relations)
360364 foreach ($ relations as $ relation => $ val ) {
361365 $ relation = $ this ->getRealFieldName ($ relation );
362366 $ type = $ this ->getFields ($ relation );
363- $ bind = $ this ->getBindAttr (self :: $ weakMap [ $ this ][ 'bind_attr ' ] , $ relation );
367+ $ bind = $ this ->getBindAttr ($ this -> getOption ( 'bind_attr ' ) , $ relation );
364368 if (!empty ($ bind )) {
365369 // 绑定关联属性
366370 $ this ->bindRelationAttr ($ val , $ bind );
@@ -384,7 +388,7 @@ protected function parseRelationData(array $relations)
384388 */
385389 protected function setRelation (string $ relation , array $ data )
386390 {
387- self :: $ weakMap [ $ this ][ 'relation ' ][ $ relation] = $ data ;
391+ $ this -> setWeakData ( 'relation ' , $ relation, $ data) ;
388392 }
389393
390394 /**
@@ -396,7 +400,7 @@ protected function setRelation(string $relation, array $data)
396400 */
397401 public function getRelation (string $ relation ): array
398402 {
399- return self :: $ weakMap [ $ this ][ 'relation ' ][ $ relation] ?? [] ;
403+ return $ this -> getWeakData ( 'relation ' , $ relation, []) ;
400404 }
401405
402406 /**
@@ -443,7 +447,7 @@ public function withAttr(string $name, callable $callback)
443447 */
444448 protected function getRealFieldName (string $ name )
445449 {
446- if (! self :: $ weakMap [ $ this ][ 'strict ' ] ) {
450+ if (false === $ this -> getOption ( 'strict ' ) ) {
447451 return Str::snake ($ name );
448452 }
449453
@@ -717,9 +721,10 @@ public function dec(string $field, float $step = 1, int $lazyTime = 0)
717721 */
718722 protected function validate (array $ data , array $ allow )
719723 {
720- if (!empty (self ::$ weakMap [$ this ]['validate ' ]) && class_exists ('think\validate ' )) {
724+ $ validater = $ this ->getOption ('validate ' );
725+ if (!empty ($ validater ) && class_exists ('think\validate ' )) {
721726 try {
722- validate (self :: $ weakMap [ $ this ][ ' validate ' ] )
727+ validate ($ validater )
723728 ->only ($ allow )
724729 ->check ($ data );
725730 } catch (ValidateException $ e ) {
@@ -832,9 +837,9 @@ public function save(array | object $data = [], $where = []): bool
832837 */
833838 protected function autoDateTime (array &$ data , bool $ update )
834839 {
835- $ dateTimeFields = [self :: $ weakMap [ $ this ][ 'update_time ' ] ];
840+ $ dateTimeFields = [$ this -> getOption ( 'update_time ' ) ];
836841 if (!$ update ) {
837- array_unshift ($ dateTimeFields , self :: $ weakMap [ $ this ][ 'create_time ' ] );
842+ array_unshift ($ dateTimeFields , $ this -> getOption ( 'create_time ' ) );
838843 }
839844
840845 foreach ($ dateTimeFields as $ field ) {
@@ -853,8 +858,9 @@ protected function autoDateTime(array &$data, bool $update)
853858 */
854859 protected function autoInsertData (array &$ data )
855860 {
856- if (!empty (self ::$ weakMap [$ this ]['auto_insert ' ])) {
857- foreach (self ::$ weakMap [$ this ]['auto_insert ' ] as $ name => $ val ) {
861+ $ autoInsert = $ this ->getOption ('auto_insert ' , []);
862+ if (!empty ($ autoInsert )) {
863+ foreach ($ autoInsert as $ name => $ val ) {
858864 $ field = is_string ($ name ) ? $ name : $ val ;
859865 if (!isset ($ data [$ field ])) {
860866 if ($ val instanceof Closure) {
@@ -950,7 +956,7 @@ protected function relationDelete(array $relations = [])
950956 */
951957 protected function getRelationKey (string $ relation )
952958 {
953- $ relationKey = self :: $ weakMap [ $ this ][ 'relation_keys ' ] ;
959+ $ relationKey = $ this -> getOption ( 'relation_keys ' , []) ;
954960 return $ relationKey [$ relation ] ?? null ;
955961 }
956962
@@ -1132,9 +1138,9 @@ public function getData(?string $name = null)
11321138 {
11331139 if ($ name ) {
11341140 $ name = $ this ->getRealFieldName ($ name );
1135- return self :: $ weakMap [ $ this ][ 'data ' ][ $ name] ?? null ;
1141+ return $ this -> getWeakData ( 'data ' , $ name) ;
11361142 }
1137- return self :: $ weakMap [ $ this ][ 'data ' ] ;
1143+ return $ this -> getOption ( 'data ' ) ;
11381144 }
11391145
11401146 /**
@@ -1149,7 +1155,7 @@ protected function setData(string $name, $value)
11491155 {
11501156 $ this ->setWeakData ('data ' , $ name , $ value );
11511157 if (isset (self ::$ weakMap [$ this ]['get ' ][$ name ])) {
1152- self :: $ weakMap [ $ this ][ 'get ' ][ $ name] = null ;
1158+ $ this -> setWeakData ( 'get ' , $ name, null ) ;
11531159 }
11541160 }
11551161
@@ -1163,7 +1169,7 @@ protected function setData(string $name, $value)
11631169 public function data (array $ data )
11641170 {
11651171 $ this ->initializeData ($ data );
1166- self :: $ weakMap [ $ this ][ 'get ' ] = [] ;
1172+ $ this -> setOption ( 'get ' , []) ;
11671173 return $ this ;
11681174 }
11691175
@@ -1174,10 +1180,10 @@ public function data(array $data)
11741180 */
11751181 public function clear ()
11761182 {
1177- self :: $ weakMap [ $ this ][ 'data ' ] = [] ;
1178- self :: $ weakMap [ $ this ][ 'origin ' ] = [] ;
1179- self :: $ weakMap [ $ this ][ 'get ' ] = [] ;
1180- self :: $ weakMap [ $ this ][ 'relation ' ] = [] ;
1183+ $ this -> setOption ( 'data ' , []) ;
1184+ $ this -> setOption ( 'origin ' , []) ;
1185+ $ this -> setOption ( 'get ' , []) ;
1186+ $ this -> setOption ( 'relation ' , []) ;
11811187 return $ this ;
11821188 }
11831189
@@ -1191,9 +1197,9 @@ public function getOrigin(?string $name = null)
11911197 {
11921198 if ($ name ) {
11931199 $ name = $ this ->getRealFieldName ($ name );
1194- return self :: $ weakMap [ $ this ][ 'origin ' ][ $ name] ?? null ;
1200+ return $ this -> getWeakData ( 'origin ' , $ name) ;
11951201 }
1196- return self :: $ weakMap [ $ this ][ 'origin ' ] ;
1202+ return $ this -> getOption ( 'origin ' ) ;
11971203 }
11981204
11991205 /**
@@ -1207,7 +1213,7 @@ public function toArray(array $allow = []): array
12071213 $ data = $ this ->getData ();
12081214 if (empty ($ allow )) {
12091215 foreach (['visible ' , 'hidden ' , 'append ' ] as $ convert ) {
1210- $ {$ convert } = self :: $ weakMap [ $ this ][ $ convert] ;
1216+ $ {$ convert } = $ this -> getOption ( $ convert) ;
12111217 foreach ($ {$ convert } as $ key => $ val ) {
12121218 if (is_string ($ key )) {
12131219 $ relation [$ key ][$ convert ] = $ val ;
@@ -1246,7 +1252,7 @@ public function toArray(array $allow = []): array
12461252 }
12471253
12481254 // 输出额外属性 必须定义获取器
1249- foreach (self :: $ weakMap [ $ this ][ 'append ' ] as $ key ) {
1255+ foreach ($ this -> getOption ( 'append ' ) as $ key ) {
12501256 $ item [$ key ] = $ this ->get ($ key );
12511257 }
12521258
@@ -1296,7 +1302,7 @@ public function set(string $name, $value)
12961302 if (is_null ($ value ) && is_subclass_of ($ type , Entity::class)) {
12971303 // 关联数据为空 设置一个空模型
12981304 $ value = new $ type ();
1299- } elseif (!($ value instanceof Entity || $ value instanceof Collection)) {
1305+ } elseif (!($ value instanceof self || $ value instanceof Collection)) {
13001306 // 类型自动转换
13011307 $ value = $ this ->readTransform ($ value , $ type );
13021308 }
@@ -1367,7 +1373,7 @@ public function get(string $name, bool $attr = true)
13671373 return self ::$ weakMap [$ this ]['get ' ][$ name ];
13681374 }
13691375
1370- if (!array_key_exists ($ name , self :: $ weakMap [ $ this ][ 'data ' ] )) {
1376+ if (!array_key_exists ($ name , $ this -> getOption ( 'data ' ) )) {
13711377 // 动态获取关联数据
13721378 $ value = $ this ->getRelationData ($ name ) ?: null ;
13731379 } else {
@@ -1513,7 +1519,7 @@ public function __get(string $name)
15131519 */
15141520 public function __set (string $ name , $ value ): void
15151521 {
1516- if ($ value instanceof Entity && $ bind = $ this ->getBindAttr (self :: $ weakMap [ $ this ][ 'bind_attr ' ] , $ name )) {
1522+ if ($ value instanceof Entity && $ bind = $ this ->getBindAttr ($ this -> getOption ( 'bind_attr ' ) , $ name )) {
15171523 // 关联属性绑定
15181524 $ this ->bindRelationAttr ($ value , $ bind );
15191525 } else {
@@ -1572,7 +1578,7 @@ public function __unset(string $name): void
15721578 {
15731579 $ name = $ this ->getRealFieldName ($ name );
15741580
1575- self :: $ weakMap [ $ this ][ 'data ' ][ $ name] = null ;
1581+ $ this -> setWeakData ( 'data ' , $ name, null ) ;
15761582 }
15771583
15781584 public function __toString ()
@@ -1583,9 +1589,9 @@ public function __toString()
15831589 public function __debugInfo ()
15841590 {
15851591 return [
1586- 'data ' => self :: $ weakMap [ $ this ][ 'data ' ] ,
1587- 'origin ' => self :: $ weakMap [ $ this ][ 'origin ' ] ,
1588- 'schema ' => self :: $ weakMap [ $ this ][ 'schema ' ] ,
1592+ 'data ' => $ this -> getOption ( 'data ' ) ,
1593+ 'origin ' => $ this -> getOption ( 'origin ' ) ,
1594+ 'schema ' => $ this -> getOption ( 'schema ' ) ,
15891595 ];
15901596 }
15911597
0 commit comments