@@ -421,41 +421,47 @@ public function loadFromSQL(string $sql, array $input = []) : bool
421
421
$ this ->empty = false ;
422
422
$ this ->loaded = true ;
423
423
424
+ $ this ->correctTypes ();
425
+
426
+ return true ;
427
+ }
428
+
429
+ protected function correctTypes () : static
430
+ {
424
431
// cast to correct values as ints, floats, etc are read in from PDO as strings
425
432
foreach (static ::$ fields as $ field => $ row )
426
433
{
427
- if (null !== $ this ->current [$ field ])
434
+ $ relationship = static ::$ virtualFields [$ field ] ?? false ;
435
+
436
+ if (\is_array ($ relationship ))
437
+ {
438
+ $ relationshipClass = \array_shift ($ relationship );
439
+ $ relationshipObject = new $ relationshipClass ($ this , $ field );
440
+ $ relationshipObject ->setValue ($ relationshipObject ->fromPHPValue ($ this ->current [$ field ], $ relationship ), $ relationship );
441
+ }
442
+ else if (\array_key_exists ($ field , $ this ->current ))
428
443
{
429
444
switch ($ row [1 ])
430
445
{
431
446
case 'int ' :
432
- if (\array_key_exists ($ field , $ this ->current ))
433
- {
434
- $ this ->current [$ field ] = (int )$ this ->current [$ field ];
435
- }
447
+ $ this ->current [$ field ] = (int )$ this ->current [$ field ];
436
448
437
449
break ;
438
450
439
451
case 'float ' :
440
- if (\array_key_exists ($ field , $ this ->current ))
441
- {
442
- $ this ->current [$ field ] = (float )$ this ->current [$ field ];
443
- }
452
+ $ this ->current [$ field ] = (float )$ this ->current [$ field ];
444
453
445
454
break ;
446
455
447
456
case 'bool ' :
448
- if (\array_key_exists ($ field , $ this ->current ))
449
- {
450
- $ this ->current [$ field ] = (bool )$ this ->current [$ field ];
451
- }
457
+ $ this ->current [$ field ] = (bool )$ this ->current [$ field ];
452
458
453
459
break ;
454
460
}
455
461
}
456
462
}
457
463
458
- return true ;
464
+ return $ this ;
459
465
}
460
466
461
467
/**
@@ -524,6 +530,8 @@ public function setEmpty() : static
524
530
$ this ->current [$ field ] = $ description [self ::DEFAULT_INDEX ] ?? null ;
525
531
}
526
532
533
+ $ this ->correctTypes ();
534
+
527
535
return $ this ;
528
536
}
529
537
@@ -543,10 +551,12 @@ public function setFrom(array $values, bool $loaded = false) : static
543
551
if (isset (static ::$ fields [$ field ]))
544
552
{
545
553
$ this ->empty = false ;
546
- $ this ->{ $ field} = $ value ;
554
+ $ this ->current [ $ field] = $ value ;
547
555
}
548
556
}
549
557
558
+ $ this ->correctTypes ();
559
+
550
560
return $ this ;
551
561
}
552
562
0 commit comments