@@ -106,7 +106,7 @@ public function equals(ActiveRecordInterface $record): bool
106
106
return false ;
107
107
}
108
108
109
- return $ this ->getTableName () === $ record ->getTableName () && $ this ->getPrimaryKey () === $ record ->getPrimaryKey ();
109
+ return $ this ->getTableName () === $ record ->getTableName () && $ this ->getPrimaryKeys () === $ record ->getPrimaryKeys ();
110
110
}
111
111
112
112
public function get (string $ propertyName ): mixed
@@ -180,25 +180,33 @@ public function oldValues(): array
180
180
return $ this ->oldValues ?? [];
181
181
}
182
182
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
188
200
{
189
201
$ keys = $ this ->primaryKey ();
190
202
191
203
if (empty ($ keys )) {
192
204
throw new Exception (
193
205
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. '
195
207
);
196
208
}
197
209
198
- if ($ asArray === false && count ($ keys ) === 1 ) {
199
- return $ this ->oldValues [$ keys [0 ]] ?? null ;
200
- }
201
-
202
210
$ values = [];
203
211
204
212
foreach ($ keys as $ name ) {
@@ -208,12 +216,31 @@ public function getOldPrimaryKey(bool $asArray = false): mixed
208
216
return $ values ;
209
217
}
210
218
211
- public function getPrimaryKey (bool $ asArray = false ): mixed
219
+ public function getPrimaryKey (): float | int | string | null
212
220
{
213
221
$ keys = $ this ->primaryKey ();
214
222
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
+ );
217
244
}
218
245
219
246
$ values = [];
@@ -554,7 +581,7 @@ public function populateRelation(string $name, array|ActiveRecordInterface|null
554
581
*/
555
582
public function refresh (): bool
556
583
{
557
- $ record = $ this ->query ()->findByPk ($ this ->getPrimaryKey ( true ));
584
+ $ record = $ this ->query ()->findByPk ($ this ->getOldPrimaryKeys ( ));
558
585
559
586
return $ this ->refreshInternal ($ record );
560
587
}
@@ -758,7 +785,7 @@ public function updateAllCounters(array $counters, array|string $condition = '',
758
785
*/
759
786
public function updateCounters (array $ counters ): bool
760
787
{
761
- if ($ this ->updateAllCounters ($ counters , $ this ->getOldPrimaryKey ( true )) === 0 ) {
788
+ if ($ this ->updateAllCounters ($ counters , $ this ->getOldPrimaryKeys ( )) === 0 ) {
762
789
return false ;
763
790
}
764
791
@@ -864,7 +891,7 @@ public function unlink(string $relationName, ActiveRecordInterface $arClass, boo
864
891
/** @psalm-var array<array-key, ActiveRecordInterface> $related */
865
892
$ related = $ this ->related [$ relationName ];
866
893
foreach ($ related as $ a => $ b ) {
867
- if ($ arClass ->getPrimaryKey () === $ b ->getPrimaryKey ()) {
894
+ if ($ arClass ->getPrimaryKeys () === $ b ->getPrimaryKeys ()) {
868
895
unset($ this ->related [$ relationName ][$ a ]);
869
896
}
870
897
}
@@ -1044,7 +1071,7 @@ protected function deleteInternal(): int
1044
1071
* We don't check the return value of deleteAll() because it is possible the record is already deleted in
1045
1072
* the database and thus the method will return 0
1046
1073
*/
1047
- $ condition = $ this ->getOldPrimaryKey ( true );
1074
+ $ condition = $ this ->getOldPrimaryKeys ( );
1048
1075
1049
1076
if ($ this instanceof OptimisticLockInterface) {
1050
1077
$ lock = $ this ->optimisticLockPropertyName ();
@@ -1130,7 +1157,7 @@ protected function updateInternal(array|null $properties = null): int
1130
1157
return 0 ;
1131
1158
}
1132
1159
1133
- $ condition = $ this ->getOldPrimaryKey ( true );
1160
+ $ condition = $ this ->getOldPrimaryKeys ( );
1134
1161
1135
1162
if ($ this instanceof OptimisticLockInterface) {
1136
1163
$ lock = $ this ->optimisticLockPropertyName ();
0 commit comments