@@ -73,6 +73,11 @@ class ManyField extends CompositeField
73
73
*/
74
74
protected $ ajaxUrl = false ;
75
75
76
+ /**
77
+ * @var string
78
+ */
79
+ protected $ manyFieldDataClass ;
80
+
76
81
/**
77
82
* Does creating a new row automatically call write to the database?
78
83
*
@@ -366,6 +371,10 @@ public function saveRecord()
366
371
$ index = Controller::curr ()->getRequest ()->requestVar ('ID ' );
367
372
$ class = Controller::curr ()->getRequest ()->requestVar ('ClassName ' );
368
373
374
+ if (!$ class ) {
375
+ $ class = $ this ->manyFieldDataClass ;
376
+ }
377
+
369
378
if (!$ class && $ this ->value ) {
370
379
$ class = $ this ->value ->dataClass ();
371
380
}
@@ -374,6 +383,10 @@ public function saveRecord()
374
383
throw new Exception ('saveRecord() must be passed an ID and ClassName ' );
375
384
}
376
385
386
+ if ($ this ->manyFieldDataClass && $ class !== $ this ->manyFieldDataClass ) {
387
+ throw new Exception ('Invalid ClassName passed ' );
388
+ }
389
+
377
390
$ record = $ class ::get ()->byId ($ index );
378
391
379
392
if (!$ record || !$ record ->canEdit ()) {
@@ -405,14 +418,22 @@ public function recordForm()
405
418
$ index = Controller::curr ()->getRequest ()->getVar ('RecordID ' );
406
419
$ class = Controller::curr ()->getRequest ()->getVar ('ClassName ' );
407
420
421
+ if (!$ class ) {
422
+ $ class = $ this ->manyFieldDataClass ;
423
+ }
424
+
408
425
if (!$ index || !$ class ) {
409
426
throw new Exception ('recordForm() must be passed an RecordID and ClassName ' );
410
427
}
411
428
429
+ if ($ this ->manyFieldDataClass && $ class !== $ this ->manyFieldDataClass ) {
430
+ throw new Exception ('Invalid ClassName passed ' );
431
+ }
432
+
412
433
$ record = $ class ::get ()->byId ($ index );
413
434
414
435
if (!$ record || !$ record ->canEdit ()) {
415
- return Controller::curr ()->httpError (400 );
436
+ return Controller::curr ()->httpError (404 );
416
437
}
417
438
418
439
$ response = new HTTPResponse ();
@@ -439,10 +460,18 @@ public function deleteRecord()
439
460
$ index = Controller::curr ()->getRequest ()->getVar ('ID ' );
440
461
$ class = Controller::curr ()->getRequest ()->getVar ('ClassName ' );
441
462
463
+ if (!$ class ) {
464
+ $ class = $ this ->manyFieldDataClass ;
465
+ }
466
+
442
467
if (!$ index || !$ class ) {
443
468
throw new Exception ('deleteRecord() must be passed an ID and ClassName ' );
444
469
}
445
470
471
+ if ($ this ->manyFieldDataClass && $ class !== $ this ->manyFieldDataClass ) {
472
+ throw new Exception ('Invalid ClassName passed ' );
473
+ }
474
+
446
475
$ record = $ class ::get ()->byId ($ index );
447
476
448
477
if (!$ record || !$ record ->canDelete ()) {
@@ -454,6 +483,18 @@ public function deleteRecord()
454
483
return $ this ->forTemplate ();
455
484
}
456
485
486
+ /**
487
+ * @param string
488
+ *
489
+ * @return self
490
+ */
491
+ public function setDataClass ($ class )
492
+ {
493
+ $ this ->manyFieldDataClass = $ class ;
494
+
495
+ return $ this ;
496
+ }
497
+
457
498
/**
458
499
* Add URL
459
500
*
@@ -634,8 +675,6 @@ public function updateRelation(DataObjectInterface $record, $delete = true)
634
675
}
635
676
636
677
$ existing = $ record ->{$ this ->name }();
637
- $ removed = [];
638
-
639
678
// if no value then we should clear everything out
640
679
if (!$ this ->value && $ this ->canRemove ) {
641
680
if ($ delete ) {
0 commit comments