21
21
use Crud \Traits \SerializeTrait ;
22
22
use Crud \Traits \ViewTrait ;
23
23
use Crud \Traits \ViewVarTrait ;
24
+ use function Cake \Core \pluginSplit ;
25
+ use function Cake \I18n \__ ;
24
26
25
27
/**
26
28
* Class RelationshipViewAction
@@ -47,7 +49,7 @@ class RelationshipsAction extends BaseAction
47
49
*
48
50
* @var array
49
51
*/
50
- protected $ _defaultConfig = [
52
+ protected array $ _defaultConfig = [
51
53
'enabled ' => true ,
52
54
'scope ' => 'entity ' ,
53
55
'findMethod ' => 'all ' ,
@@ -150,7 +152,7 @@ public function checkAllowed(): void
150
152
protected function _findRelations (Subject $ subject ): EntityInterface
151
153
{
152
154
$ relationName = $ this ->_request ()->getParam ('type ' );
153
- $ table = $ this ->_table ();
155
+ $ table = $ this ->_controller ()-> fetchTable ();
154
156
$ association = $ table ->getAssociation ($ relationName );
155
157
$ targetTable = $ association ->getTarget ();
156
158
@@ -188,7 +190,7 @@ protected function _findRelations(Subject $subject): EntityInterface
188
190
->where (
189
191
[
190
192
$ table ->aliasField ($ primaryKey ) => $ foreignKeyParam ,
191
- ]
193
+ ],
192
194
)
193
195
->contain ([
194
196
$ relationName => [
@@ -202,7 +204,7 @@ protected function _findRelations(Subject $subject): EntityInterface
202
204
'association ' => $ association ,
203
205
'repository ' => $ table ,
204
206
'query ' => $ primaryQuery ,
205
- ]
207
+ ],
206
208
);
207
209
$ this ->_trigger ('beforeFind ' , $ subject );
208
210
$ entity = $ subject ->query ->first ();
@@ -239,7 +241,7 @@ protected function _get(): void
239
241
*
240
242
* @return void
241
243
*/
242
- protected function _delete ()
244
+ protected function _delete (): void
243
245
{
244
246
$ subject = $ this ->_subject ();
245
247
$ request = $ this ->_request ();
@@ -272,7 +274,7 @@ protected function _delete()
272
274
$ idsToDelete = (array )Hash::extract ($ data , '{n}.id ' );
273
275
$ foreignRecords = $ entity ->$ property ;
274
276
$ entity ->$ property = [];
275
- foreach ($ foreignRecords as $ key => $ foreignRecord ) {
277
+ foreach ($ foreignRecords as $ foreignRecord ) {
276
278
if (!in_array ($ foreignRecord ->id , $ idsToDelete , false )) {
277
279
$ entity ->{$ property }[] = $ foreignRecord ;
278
280
}
@@ -284,7 +286,7 @@ protected function _delete()
284
286
$ association ->setSaveStrategy ('replace ' );
285
287
}
286
288
$ saveMethod = $ this ->saveMethod ();
287
- if ($ this ->_table ()->$ saveMethod ($ entity , $ this ->saveOptions ())) {
289
+ if ($ this ->_controller ()-> fetchTable ()->$ saveMethod ($ entity , $ this ->saveOptions ())) {
288
290
$ this ->_success ($ subject );
289
291
290
292
return ;
@@ -298,7 +300,7 @@ protected function _delete()
298
300
*
299
301
* @return void
300
302
*/
301
- protected function _post ()
303
+ protected function _post (): void
302
304
{
303
305
$ subject = $ this ->_subject ();
304
306
$ request = $ this ->_request ();
@@ -333,7 +335,7 @@ protected function _post()
333
335
$ this ->_trigger ('beforeSave ' , $ subject );
334
336
335
337
$ saveMethod = $ this ->saveMethod ();
336
- if ($ this ->_table ()->$ saveMethod ($ entity , $ this ->saveOptions ())) {
338
+ if ($ this ->_controller ()-> fetchTable ()->$ saveMethod ($ entity , $ this ->saveOptions ())) {
337
339
$ this ->_success ($ subject );
338
340
339
341
return ;
@@ -347,7 +349,7 @@ protected function _post()
347
349
*
348
350
* @return void
349
351
*/
350
- protected function _patch ()
352
+ protected function _patch (): void
351
353
{
352
354
$ subject = $ this ->_subject ();
353
355
$ request = $ this ->_request ();
@@ -363,7 +365,6 @@ protected function _patch()
363
365
364
366
if (in_array ($ association ->type (), [Association::MANY_TO_ONE , Association::ONE_TO_ONE ], true )) {
365
367
//Set the relationship to the corresponding entity
366
- /** @psalm-suppress TypeDoesNotContainNull */
367
368
if (array_key_exists ('id ' , $ data )) {
368
369
$ entity ->{$ property } = $ foreignTable ->get ($ data ['id ' ]);
369
370
} elseif ($ data === null ) {
@@ -382,7 +383,7 @@ protected function _patch()
382
383
$ association ->setSaveStrategy ('replace ' );
383
384
}
384
385
$ saveMethod = $ this ->saveMethod ();
385
- if ($ this ->_table ()->$ saveMethod ($ entity , $ this ->saveOptions ())) {
386
+ if ($ this ->_controller ()-> fetchTable ()->$ saveMethod ($ entity , $ this ->saveOptions ())) {
386
387
$ this ->_success ($ subject );
387
388
388
389
return ;
@@ -442,21 +443,21 @@ protected function getForeignRecords(array $data, Association $association): arr
442
443
->where (
443
444
[
444
445
$ association ->aliasField ($ associationPrimaryKey ) . ' in ' => $ idsToAdd ,
445
- ]
446
+ ],
446
447
)
447
448
->all ();
448
449
449
450
if (count ($ idsToAdd ) !== count ($ foreignRecords )) {
450
451
$ foundIds = $ foreignRecords ->extract (
451
452
static function ($ record ) {
452
453
return $ record ->id ;
453
- }
454
+ },
454
455
)
455
456
->toArray ();
456
457
$ missingIds = array_diff ($ idsToAdd , $ foundIds );
457
458
458
459
throw new RecordNotFoundException (
459
- __ ('Not all requested records could be found. Missing IDs are {0} ' , implode (', ' , $ missingIds ))
460
+ __ ('Not all requested records could be found. Missing IDs are {0} ' , implode (', ' , $ missingIds )),
460
461
);
461
462
}
462
463
0 commit comments