@@ -63,33 +63,22 @@ public function __construct(int|array|null|string $parameter = null)
6363
6464 switch ($ type )
6565 {
66- case 'string ' :
67-
68- if (1 == \count (static ::$ primaryKeys ))
69- {
70- $ this ->read ($ parameter );
71- }
72- else
73- {
74- throw new \PHPFUI \ORM \Exception (static ::class . ' has no string primary key ' );
75- }
76-
77- break ;
78-
7966 case 'integer ' :
67+ $ type = 'int ' ;
68+ // Intentionally fall through
69+ case 'string ' :
8070
81- if (1 == \count (static ::$ primaryKeys ) && ' int ' == static ::$ fields [\array_key_first ( static ::$ primaryKeys) ][self ::PHP_TYPE_INDEX ])
71+ if (1 == \count (static ::$ primaryKeys ) && $ type == static ::$ fields [static ::$ primaryKeys[ 0 ] ][self ::PHP_TYPE_INDEX ])
8272 {
8373 $ this ->read ($ parameter );
8474 }
8575 else
8676 {
87- throw new \PHPFUI \ORM \Exception (static ::class . ' does not have an integer primary key ' );
77+ throw new \PHPFUI \ORM \Exception (static ::class . ' has no ' . $ type . ' primary key ' );
8878 }
8979
9080 break ;
9181
92-
9382 case 'array ' :
9483
9584 $ this ->read ($ parameter );
@@ -360,7 +349,7 @@ public function getLength(string $field) : int
360349 }
361350
362351 /**
363- * @return array primary keys
352+ * @return array<string> primary keys
364353 */
365354 public static function getPrimaryKeys () : array
366355 {
@@ -374,7 +363,7 @@ public function getPrimaryKeyValues() : array
374363 {
375364 $ retVal = [];
376365
377- foreach (static ::$ primaryKeys as $ key => $ junk )
366+ foreach (static ::$ primaryKeys as $ key )
378367 {
379368 $ retVal [$ key ] = $ this ->current [$ key ] ?? null ;
380369 }
@@ -518,7 +507,7 @@ public function reload() : bool
518507 {
519508 $ keys = [];
520509
521- foreach (static ::$ primaryKeys as $ key => $ junk )
510+ foreach (static ::$ primaryKeys as $ key )
522511 {
523512 if (\array_key_exists ($ key , $ this ->current ))
524513 {
@@ -608,7 +597,7 @@ public function update() : bool
608597 {
609598 if (isset (static ::$ fields [$ field ]))
610599 {
611- if (! isset ( static ::$ primaryKeys[ $ field ] ))
600+ if (! \in_array ( $ field , static ::$ primaryKeys ))
612601 {
613602 if (empty ($ value ) && \in_array (static ::$ fields [$ field ][self ::MYSQL_TYPE_INDEX ], $ dateTimes ))
614603 {
@@ -786,15 +775,15 @@ private function buildWhere(array|int|string $key, array &$input) : string
786775
787776 if (! \is_array ($ key ))
788777 {
789- $ key = [\array_key_first ( static ::$ primaryKeys) => $ key ];
778+ $ key = [static ::$ primaryKeys[ 0 ] => $ key ];
790779 }
791780 else
792781 { // if all primary keys are set, then use primary keys only
793782
794783 $ keys = [];
795784 $ all = true ;
796785
797- foreach (static ::$ primaryKeys as $ keyField => $ junk )
786+ foreach (static ::$ primaryKeys as $ keyField )
798787 {
799788 if (! isset ($ key [$ keyField ]))
800789 {
@@ -854,7 +843,7 @@ private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : i
854843 continue ;
855844 }
856845
857- if (! static ::$ autoIncrement || ! (isset ( static ::$ primaryKeys[ $ key ] ) && empty ($ value )))
846+ if (! static ::$ autoIncrement || ! (\in_array ( $ key , static ::$ primaryKeys ) && empty ($ value )))
858847 {
859848 $ sql .= $ comma . '` ' . $ key . '` ' ;
860849 $ input [] = $ value ;
@@ -883,7 +872,7 @@ private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : i
883872 continue ;
884873 }
885874
886- if (! isset ( static ::$ primaryKeys[ $ key ] ))
875+ if (! \in_array ( $ key , static ::$ primaryKeys ))
887876 {
888877 $ updateSql .= $ comma . '` ' . $ key . '` = ? ' ;
889878 $ input [] = $ value ;
@@ -906,7 +895,7 @@ private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : i
906895
907896 if (static ::$ autoIncrement && $ returnValue )
908897 {
909- $ this ->current [\array_key_first ( static ::$ primaryKeys)] = $ returnValue = (int )\PHPFUI \ORM ::lastInsertId (\array_key_first ( static ::$ primaryKeys) );
898+ $ this ->current [static ::$ primaryKeys[ 0 ]] = $ returnValue = (int )\PHPFUI \ORM ::lastInsertId (static ::$ primaryKeys[ 0 ] );
910899 }
911900
912901 $ this ->loaded = true ; // record is effectively read from the database now
0 commit comments