@@ -116,6 +116,7 @@ public function testJoinSaving1(): void
116
116
$ user2 = $ user ->createEntity ();
117
117
$ user2 ->set ('name ' , 'John ' );
118
118
$ user2 ->set ('contact_phone ' , '+123 ' );
119
+ $ j ->allowDangerousForeignTableUpdate = true ;
119
120
$ user2 ->save ();
120
121
121
122
self ::assertSame (1 , $ user2 ->getId ());
@@ -169,6 +170,7 @@ public function testJoinSaving2(): void
169
170
$ user2 = $ user ->createEntity ();
170
171
$ user2 ->set ('name ' , 'John ' );
171
172
$ user2 ->set ('contact_phone ' , '+123 ' );
173
+ $ j ->allowDangerousForeignTableUpdate = true ;
172
174
$ user2 ->save ();
173
175
174
176
self ::assertSame (1 , $ user2 ->getId ());
@@ -239,6 +241,7 @@ public function testJoinSaving3(): void
239
241
$ user = $ user ->createEntity ();
240
242
$ user ->set ('name ' , 'John ' );
241
243
$ user ->set ('contact_phone ' , '+123 ' );
244
+ $ j ->allowDangerousForeignTableUpdate = true ;
242
245
$ user ->save ();
243
246
244
247
self ::assertSame ([
@@ -313,6 +316,7 @@ public function testJoinUpdate(): void
313
316
$ user2 = $ user ->load (1 );
314
317
$ user2 ->set ('name ' , 'John 2 ' );
315
318
$ user2 ->set ('contact_phone ' , '+555 ' );
319
+ $ j ->allowDangerousForeignTableUpdate = true ;
316
320
$ user2 ->save ();
317
321
318
322
self ::assertSame ([
@@ -405,6 +409,7 @@ public function testJoinDelete(): void
405
409
$ j ->addField ('contact_phone ' );
406
410
407
411
$ user = $ user ->load (3 );
412
+ $ j ->allowDangerousForeignTableUpdate = true ;
408
413
$ user ->delete ();
409
414
410
415
self ::assertSame ([
@@ -432,6 +437,20 @@ public function testJoinDelete(): void
432
437
}
433
438
}
434
439
440
+ public function testDangerousForeignTableUpdateException (): void
441
+ {
442
+ $ user = new Model ($ this ->db , ['table ' => 'user ' ]);
443
+ $ j = $ user ->join ('contact ' );
444
+ $ j ->addField ('phone ' );
445
+
446
+ $ user2 = $ user ->createEntity ();
447
+ $ user2 ->set ('phone ' , '+555 ' );
448
+
449
+ $ this ->expectException (Exception::class);
450
+ $ this ->expectExceptionMessage ('Model is read-only ' );
451
+ $ user2 ->save ();
452
+ }
453
+
435
454
public function testDoubleSaveHook (): void
436
455
{
437
456
$ this ->setDb ([
@@ -458,6 +477,7 @@ public function testDoubleSaveHook(): void
458
477
459
478
$ user = $ user ->createEntity ();
460
479
$ user ->set ('name ' , 'John ' );
480
+ $ j ->allowDangerousForeignTableUpdate = true ;
461
481
$ user ->save ();
462
482
463
483
self ::assertSame ([
@@ -505,6 +525,8 @@ public function testDoubleJoin(): void
505
525
506
526
$ user2 = $ user ->load (10 );
507
527
self ::assertSame (['id ' => 10 , 'contact_id ' => 100 , 'name ' => 'John 2 ' , 'contact_phone ' => '+555 ' , 'country_id ' => 1 , 'country_name ' => 'UK ' ], $ user2 ->get ());
528
+ $ jContact ->allowDangerousForeignTableUpdate = true ;
529
+ $ jCountry ->allowDangerousForeignTableUpdate = true ;
508
530
$ user2 ->delete ();
509
531
510
532
$ user2 = $ user ->loadBy ('country_name ' , 'US ' );
@@ -572,6 +594,8 @@ public function testDoubleReverseJoin(): void
572
594
573
595
$ country2 = $ country ->load (1 );
574
596
self ::assertSame (['id ' => 1 , 'name ' => 'UK ' , 'contact_phone ' => '+555 ' , 'contact_id ' => 100 , 'user_name ' => 'John 2 ' ], $ country2 ->get ());
597
+ $ jContact ->allowDangerousForeignTableUpdate = true ;
598
+ $ jUser ->allowDangerousForeignTableUpdate = true ;
575
599
$ country2 ->delete ();
576
600
577
601
$ country2 = $ country ->loadBy ('user_name ' , 'XX ' );
@@ -706,6 +730,7 @@ public function testJoinReverseOneOnOne(): void
706
730
self ::assertSame (['id ' => 20 , 'name ' => 'Peter ' , 'notes ' => 'second note ' ], $ m ->get ());
707
731
708
732
// update loaded record
733
+ $ j ->allowDangerousForeignTableUpdate = true ;
709
734
$ m ->save (['name ' => 'Mark ' , 'notes ' => '2nd note ' ]);
710
735
$ m = $ user ->load (20 );
711
736
self ::assertSame (['id ' => 20 , 'name ' => 'Mark ' , 'notes ' => '2nd note ' ], $ m ->get ());
@@ -726,6 +751,7 @@ public function testJoinReverseOneOnOne(): void
726
751
$ j ->addField ('notes ' );
727
752
728
753
// insert new record
754
+ $ j ->allowDangerousForeignTableUpdate = true ;
729
755
$ m = $ user ->createEntity ()->save (['name ' => 'Olaf ' , 'notes ' => '4th note ' ]);
730
756
$ m = $ user ->load (22 );
731
757
self ::assertSame (['id ' => 22 , 'name ' => 'Olaf ' , 'notes ' => '4th note ' ], $ m ->get ());
@@ -741,6 +767,7 @@ public function testJoinReverseOneOnOne(): void
741
767
$ j ->addField ('notes ' );
742
768
743
769
// insert new record
770
+ $ j ->allowDangerousForeignTableUpdate = true ;
744
771
$ m = $ user ->createEntity ()->save (['name ' => 'Chris ' , 'notes ' => '5th note ' ]);
745
772
$ m = $ user ->load (23 );
746
773
self ::assertSame (['id ' => 23 , 'name ' => 'Chris ' , 'notes ' => '5th note ' ], $ m ->get ());
@@ -788,6 +815,8 @@ public function testJoinActualFieldNamesAndPrefix(): void
788
815
$ user2 ->set ('name ' , 'John 2 ' );
789
816
$ user2 ->set ('j1_phone ' , '+555 ' );
790
817
$ user2 ->set ('j2_salary ' , 111 );
818
+ $ j ->allowDangerousForeignTableUpdate = true ;
819
+ $ j2 ->allowDangerousForeignTableUpdate = true ;
791
820
$ user2 ->save ();
792
821
793
822
self ::{'assertEquals ' }([
@@ -869,6 +898,7 @@ protected function setupJoinWithNonDefaultForeignIdField(array $joinDefaults = [
869
898
'foreignIdField ' => 'uid ' ,
870
899
], $ joinDefaults ));
871
900
$ this ->createMigrator ()->createForeignKey ($ j );
901
+ $ j ->allowDangerousForeignTableUpdate = true ;
872
902
$ j ->addField ('contact_phone ' );
873
903
874
904
return [$ masterModel , $ joinedModel , $ user ];
0 commit comments