@@ -146,11 +146,16 @@ protected function getOption(string $name, $default = null)
146
146
return self ::$ weakMap [$ this ][$ name ] ?? $ default ;
147
147
}
148
148
149
- protected function setWeakData ($ key , $ name , $ value )
149
+ private function setWeakData ($ key , $ name , $ value )
150
150
{
151
151
self ::$ weakMap [$ this ][$ key ][$ name ] = $ value ;
152
152
}
153
153
154
+ private function getWeakData ($ key , $ name , $ default = null )
155
+ {
156
+ return self ::$ weakMap [$ this ][$ key ][$ name ] ?? $ default ;
157
+ }
158
+
154
159
/**
155
160
* 获取对应表名(仅限简单模型).
156
161
*
@@ -213,7 +218,7 @@ private function getSimpleModel()
213
218
{
214
219
return Db::newQuery ()
215
220
->name ($ this ->getTableName ())
216
- ->pk (self :: $ weakMap [ $ this ][ 'pk ' ] );
221
+ ->pk ($ this -> getOption ( 'pk ' ) );
217
222
}
218
223
219
224
/**
@@ -225,7 +230,8 @@ private function getSimpleModel()
225
230
*/
226
231
protected function getFields (?string $ field = null )
227
232
{
228
- if (empty (self ::$ weakMap [$ this ]['schema ' ])) {
233
+ $ schema = $ this ->getOption ('schema ' );
234
+ if (empty ($ schema )) {
229
235
if ($ this ->isView () || $ this ->isVirtual ()) {
230
236
$ schema = $ this ->getOption ('type ' , []);
231
237
} else {
@@ -236,8 +242,6 @@ protected function getFields(?string $field = null)
236
242
}
237
243
238
244
$ this ->setOption ('schema ' , $ schema );
239
- } else {
240
- $ schema = $ this ->getOption ('schema ' );
241
245
}
242
246
243
247
if ($ field ) {
@@ -282,7 +286,7 @@ protected function parseValidate(): string
282
286
*/
283
287
public function model ()
284
288
{
285
- return self :: $ weakMap [ $ this ][ 'model ' ] ->schema (self :: $ weakMap [ $ this ][ 'schema ' ] );
289
+ return $ this -> getOption ( 'model ' ) ->schema ($ this -> getOption ( 'schema ' ) );
286
290
}
287
291
288
292
/**
@@ -303,7 +307,7 @@ protected function initializeData(array | object $data, bool $fromSave = false)
303
307
304
308
// 实体模型赋值
305
309
foreach ($ data as $ name => $ val ) {
306
- if (in_array ($ name , self :: $ weakMap [ $ this ][ 'disuse ' ] )) {
310
+ if (in_array ($ name , $ this -> getOption ( 'disuse ' ) )) {
307
311
// 废弃字段
308
312
continue ;
309
313
}
@@ -360,7 +364,7 @@ protected function parseRelationData(array $relations)
360
364
foreach ($ relations as $ relation => $ val ) {
361
365
$ relation = $ this ->getRealFieldName ($ relation );
362
366
$ type = $ this ->getFields ($ relation );
363
- $ bind = $ this ->getBindAttr (self :: $ weakMap [ $ this ][ 'bind_attr ' ] , $ relation );
367
+ $ bind = $ this ->getBindAttr ($ this -> getOption ( 'bind_attr ' ) , $ relation );
364
368
if (!empty ($ bind )) {
365
369
// 绑定关联属性
366
370
$ this ->bindRelationAttr ($ val , $ bind );
@@ -384,7 +388,7 @@ protected function parseRelationData(array $relations)
384
388
*/
385
389
protected function setRelation (string $ relation , array $ data )
386
390
{
387
- self :: $ weakMap [ $ this ][ 'relation ' ][ $ relation] = $ data ;
391
+ $ this -> setWeakData ( 'relation ' , $ relation, $ data) ;
388
392
}
389
393
390
394
/**
@@ -396,7 +400,7 @@ protected function setRelation(string $relation, array $data)
396
400
*/
397
401
public function getRelation (string $ relation ): array
398
402
{
399
- return self :: $ weakMap [ $ this ][ 'relation ' ][ $ relation] ?? [] ;
403
+ return $ this -> getWeakData ( 'relation ' , $ relation, []) ;
400
404
}
401
405
402
406
/**
@@ -443,7 +447,7 @@ public function withAttr(string $name, callable $callback)
443
447
*/
444
448
protected function getRealFieldName (string $ name )
445
449
{
446
- if (! self :: $ weakMap [ $ this ][ 'strict ' ] ) {
450
+ if (false === $ this -> getOption ( 'strict ' ) ) {
447
451
return Str::snake ($ name );
448
452
}
449
453
@@ -717,9 +721,10 @@ public function dec(string $field, float $step = 1, int $lazyTime = 0)
717
721
*/
718
722
protected function validate (array $ data , array $ allow )
719
723
{
720
- if (!empty (self ::$ weakMap [$ this ]['validate ' ]) && class_exists ('think\validate ' )) {
724
+ $ validater = $ this ->getOption ('validate ' );
725
+ if (!empty ($ validater ) && class_exists ('think\validate ' )) {
721
726
try {
722
- validate (self :: $ weakMap [ $ this ][ ' validate ' ] )
727
+ validate ($ validater )
723
728
->only ($ allow )
724
729
->check ($ data );
725
730
} catch (ValidateException $ e ) {
@@ -832,9 +837,9 @@ public function save(array | object $data = [], $where = []): bool
832
837
*/
833
838
protected function autoDateTime (array &$ data , bool $ update )
834
839
{
835
- $ dateTimeFields = [self :: $ weakMap [ $ this ][ 'update_time ' ] ];
840
+ $ dateTimeFields = [$ this -> getOption ( 'update_time ' ) ];
836
841
if (!$ update ) {
837
- array_unshift ($ dateTimeFields , self :: $ weakMap [ $ this ][ 'create_time ' ] );
842
+ array_unshift ($ dateTimeFields , $ this -> getOption ( 'create_time ' ) );
838
843
}
839
844
840
845
foreach ($ dateTimeFields as $ field ) {
@@ -853,8 +858,9 @@ protected function autoDateTime(array &$data, bool $update)
853
858
*/
854
859
protected function autoInsertData (array &$ data )
855
860
{
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 ) {
858
864
$ field = is_string ($ name ) ? $ name : $ val ;
859
865
if (!isset ($ data [$ field ])) {
860
866
if ($ val instanceof Closure) {
@@ -950,7 +956,7 @@ protected function relationDelete(array $relations = [])
950
956
*/
951
957
protected function getRelationKey (string $ relation )
952
958
{
953
- $ relationKey = self :: $ weakMap [ $ this ][ 'relation_keys ' ] ;
959
+ $ relationKey = $ this -> getOption ( 'relation_keys ' , []) ;
954
960
return $ relationKey [$ relation ] ?? null ;
955
961
}
956
962
@@ -1132,9 +1138,9 @@ public function getData(?string $name = null)
1132
1138
{
1133
1139
if ($ name ) {
1134
1140
$ name = $ this ->getRealFieldName ($ name );
1135
- return self :: $ weakMap [ $ this ][ 'data ' ][ $ name] ?? null ;
1141
+ return $ this -> getWeakData ( 'data ' , $ name) ;
1136
1142
}
1137
- return self :: $ weakMap [ $ this ][ 'data ' ] ;
1143
+ return $ this -> getOption ( 'data ' ) ;
1138
1144
}
1139
1145
1140
1146
/**
@@ -1149,7 +1155,7 @@ protected function setData(string $name, $value)
1149
1155
{
1150
1156
$ this ->setWeakData ('data ' , $ name , $ value );
1151
1157
if (isset (self ::$ weakMap [$ this ]['get ' ][$ name ])) {
1152
- self :: $ weakMap [ $ this ][ 'get ' ][ $ name] = null ;
1158
+ $ this -> setWeakData ( 'get ' , $ name, null ) ;
1153
1159
}
1154
1160
}
1155
1161
@@ -1163,7 +1169,7 @@ protected function setData(string $name, $value)
1163
1169
public function data (array $ data )
1164
1170
{
1165
1171
$ this ->initializeData ($ data );
1166
- self :: $ weakMap [ $ this ][ 'get ' ] = [] ;
1172
+ $ this -> setOption ( 'get ' , []) ;
1167
1173
return $ this ;
1168
1174
}
1169
1175
@@ -1174,10 +1180,10 @@ public function data(array $data)
1174
1180
*/
1175
1181
public function clear ()
1176
1182
{
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 ' , []) ;
1181
1187
return $ this ;
1182
1188
}
1183
1189
@@ -1191,9 +1197,9 @@ public function getOrigin(?string $name = null)
1191
1197
{
1192
1198
if ($ name ) {
1193
1199
$ name = $ this ->getRealFieldName ($ name );
1194
- return self :: $ weakMap [ $ this ][ 'origin ' ][ $ name] ?? null ;
1200
+ return $ this -> getWeakData ( 'origin ' , $ name) ;
1195
1201
}
1196
- return self :: $ weakMap [ $ this ][ 'origin ' ] ;
1202
+ return $ this -> getOption ( 'origin ' ) ;
1197
1203
}
1198
1204
1199
1205
/**
@@ -1207,7 +1213,7 @@ public function toArray(array $allow = []): array
1207
1213
$ data = $ this ->getData ();
1208
1214
if (empty ($ allow )) {
1209
1215
foreach (['visible ' , 'hidden ' , 'append ' ] as $ convert ) {
1210
- $ {$ convert } = self :: $ weakMap [ $ this ][ $ convert] ;
1216
+ $ {$ convert } = $ this -> getOption ( $ convert) ;
1211
1217
foreach ($ {$ convert } as $ key => $ val ) {
1212
1218
if (is_string ($ key )) {
1213
1219
$ relation [$ key ][$ convert ] = $ val ;
@@ -1246,7 +1252,7 @@ public function toArray(array $allow = []): array
1246
1252
}
1247
1253
1248
1254
// 输出额外属性 必须定义获取器
1249
- foreach (self :: $ weakMap [ $ this ][ 'append ' ] as $ key ) {
1255
+ foreach ($ this -> getOption ( 'append ' ) as $ key ) {
1250
1256
$ item [$ key ] = $ this ->get ($ key );
1251
1257
}
1252
1258
@@ -1296,7 +1302,7 @@ public function set(string $name, $value)
1296
1302
if (is_null ($ value ) && is_subclass_of ($ type , Entity::class)) {
1297
1303
// 关联数据为空 设置一个空模型
1298
1304
$ value = new $ type ();
1299
- } elseif (!($ value instanceof Entity || $ value instanceof Collection)) {
1305
+ } elseif (!($ value instanceof self || $ value instanceof Collection)) {
1300
1306
// 类型自动转换
1301
1307
$ value = $ this ->readTransform ($ value , $ type );
1302
1308
}
@@ -1367,7 +1373,7 @@ public function get(string $name, bool $attr = true)
1367
1373
return self ::$ weakMap [$ this ]['get ' ][$ name ];
1368
1374
}
1369
1375
1370
- if (!array_key_exists ($ name , self :: $ weakMap [ $ this ][ 'data ' ] )) {
1376
+ if (!array_key_exists ($ name , $ this -> getOption ( 'data ' ) )) {
1371
1377
// 动态获取关联数据
1372
1378
$ value = $ this ->getRelationData ($ name ) ?: null ;
1373
1379
} else {
@@ -1513,7 +1519,7 @@ public function __get(string $name)
1513
1519
*/
1514
1520
public function __set (string $ name , $ value ): void
1515
1521
{
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 )) {
1517
1523
// 关联属性绑定
1518
1524
$ this ->bindRelationAttr ($ value , $ bind );
1519
1525
} else {
@@ -1572,7 +1578,7 @@ public function __unset(string $name): void
1572
1578
{
1573
1579
$ name = $ this ->getRealFieldName ($ name );
1574
1580
1575
- self :: $ weakMap [ $ this ][ 'data ' ][ $ name] = null ;
1581
+ $ this -> setWeakData ( 'data ' , $ name, null ) ;
1576
1582
}
1577
1583
1578
1584
public function __toString ()
@@ -1583,9 +1589,9 @@ public function __toString()
1583
1589
public function __debugInfo ()
1584
1590
{
1585
1591
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 ' ) ,
1589
1595
];
1590
1596
}
1591
1597
0 commit comments