@@ -106,7 +106,7 @@ public function equals(ActiveRecordInterface $record): bool
106106 return false ;
107107 }
108108
109- return $ this ->getTableName () === $ record ->getTableName () && $ this ->getPrimaryKey () === $ record ->getPrimaryKey ();
109+ return $ this ->getTableName () === $ record ->getTableName () && $ this ->getPrimaryKeys () === $ record ->getPrimaryKeys ();
110110 }
111111
112112 public function get (string $ propertyName ): mixed
@@ -180,25 +180,33 @@ public function oldValues(): array
180180 return $ this ->oldValues ?? [];
181181 }
182182
183- /**
184- * @throws InvalidConfigException
185- * @throws Exception
186- */
187- public function getOldPrimaryKey (bool $ asArray = false ): mixed
183+ public function getOldPrimaryKey (): float |int |string |null
184+ {
185+ $ keys = $ this ->primaryKey ();
186+
187+ return match (count ($ keys )) {
188+ 1 => $ this ->oldValues [$ keys [0 ]] ?? null ,
189+ 0 => throw new Exception (
190+ static ::class . ' does not have a primary key. You should either define a primary key for '
191+ . $ this ->getTableName () . ' table or override the primaryKey() method. '
192+ ),
193+ default => throw new Exception (
194+ static ::class . ' has multiple primary keys. Use getOldPrimaryKeys() method instead. '
195+ ),
196+ };
197+ }
198+
199+ public function getOldPrimaryKeys (): array
188200 {
189201 $ keys = $ this ->primaryKey ();
190202
191203 if (empty ($ keys )) {
192204 throw new Exception (
193205 static ::class . ' does not have a primary key. You should either define a primary key for '
194- . ' the corresponding table or override the primaryKey() method. '
206+ . $ this -> getTableName () . ' table or override the primaryKey() method. '
195207 );
196208 }
197209
198- if ($ asArray === false && count ($ keys ) === 1 ) {
199- return $ this ->oldValues [$ keys [0 ]] ?? null ;
200- }
201-
202210 $ values = [];
203211
204212 foreach ($ keys as $ name ) {
@@ -208,12 +216,31 @@ public function getOldPrimaryKey(bool $asArray = false): mixed
208216 return $ values ;
209217 }
210218
211- public function getPrimaryKey (bool $ asArray = false ): mixed
219+ public function getPrimaryKey (): float | int | string | null
212220 {
213221 $ keys = $ this ->primaryKey ();
214222
215- if ($ asArray === false && count ($ keys ) === 1 ) {
216- return $ this ->get ($ keys [0 ]);
223+ return match (count ($ keys )) {
224+ 1 => $ this ->get ($ keys [0 ]),
225+ 0 => throw new Exception (
226+ static ::class . ' does not have a primary key. You should either define a primary key for '
227+ . $ this ->getTableName () . ' table or override the primaryKey() method. '
228+ ),
229+ default => throw new Exception (
230+ static ::class . ' has multiple primary keys. Use getPrimaryKeys() method instead. '
231+ ),
232+ };
233+ }
234+
235+ public function getPrimaryKeys (): array
236+ {
237+ $ keys = $ this ->primaryKey ();
238+
239+ if (empty ($ keys )) {
240+ throw new Exception (
241+ static ::class . ' does not have a primary key. You should either define a primary key for '
242+ . $ this ->getTableName () . ' table or override the primaryKey() method. '
243+ );
217244 }
218245
219246 $ values = [];
@@ -554,7 +581,7 @@ public function populateRelation(string $name, array|ActiveRecordInterface|null
554581 */
555582 public function refresh (): bool
556583 {
557- $ record = $ this ->query ()->findByPk ($ this ->getPrimaryKey ( true ));
584+ $ record = $ this ->query ()->findByPk ($ this ->getOldPrimaryKeys ( ));
558585
559586 return $ this ->refreshInternal ($ record );
560587 }
@@ -758,7 +785,7 @@ public function updateAllCounters(array $counters, array|string $condition = '',
758785 */
759786 public function updateCounters (array $ counters ): bool
760787 {
761- if ($ this ->updateAllCounters ($ counters , $ this ->getOldPrimaryKey ( true )) === 0 ) {
788+ if ($ this ->updateAllCounters ($ counters , $ this ->getOldPrimaryKeys ( )) === 0 ) {
762789 return false ;
763790 }
764791
@@ -864,7 +891,7 @@ public function unlink(string $relationName, ActiveRecordInterface $arClass, boo
864891 /** @psalm-var array<array-key, ActiveRecordInterface> $related */
865892 $ related = $ this ->related [$ relationName ];
866893 foreach ($ related as $ a => $ b ) {
867- if ($ arClass ->getPrimaryKey () === $ b ->getPrimaryKey ()) {
894+ if ($ arClass ->getPrimaryKeys () === $ b ->getPrimaryKeys ()) {
868895 unset($ this ->related [$ relationName ][$ a ]);
869896 }
870897 }
@@ -1044,7 +1071,7 @@ protected function deleteInternal(): int
10441071 * We don't check the return value of deleteAll() because it is possible the record is already deleted in
10451072 * the database and thus the method will return 0
10461073 */
1047- $ condition = $ this ->getOldPrimaryKey ( true );
1074+ $ condition = $ this ->getOldPrimaryKeys ( );
10481075
10491076 if ($ this instanceof OptimisticLockInterface) {
10501077 $ lock = $ this ->optimisticLockPropertyName ();
@@ -1130,7 +1157,7 @@ protected function updateInternal(array|null $properties = null): int
11301157 return 0 ;
11311158 }
11321159
1133- $ condition = $ this ->getOldPrimaryKey ( true );
1160+ $ condition = $ this ->getOldPrimaryKeys ( );
11341161
11351162 if ($ this instanceof OptimisticLockInterface) {
11361163 $ lock = $ this ->optimisticLockPropertyName ();
0 commit comments