14
14
use SilverStripe \Core \Injector \Injector ;
15
15
use SilverStripe \Control \HTTPResponse ;
16
16
use Exception ;
17
+ use SilverStripe \ORM \SS_List ;
17
18
18
19
class ManyField extends CompositeField
19
20
{
@@ -379,15 +380,19 @@ public function saveRecord()
379
380
$ class = $ this ->value ->dataClass ();
380
381
}
381
382
382
- if (!$ index || ! $ class ) {
383
- throw new Exception ('saveRecord() must be passed an ID and ClassName ' );
383
+ if (!$ class ) {
384
+ throw new Exception ('saveRecord() must be passed a ClassName ' );
384
385
}
385
386
386
387
if ($ this ->manyFieldDataClass && $ class !== $ this ->manyFieldDataClass ) {
387
388
throw new Exception ('Invalid ClassName passed ' );
388
389
}
389
390
390
- $ record = $ class ::get ()->byId ($ index );
391
+ if (!$ index ) {
392
+ $ record = $ class ::create ();
393
+ } else {
394
+ $ record = $ class ::get ()->byId ($ index );
395
+ }
391
396
392
397
if (!$ record || !$ record ->canEdit ()) {
393
398
return Controller::curr ()->httpError (400 );
@@ -649,10 +654,17 @@ public function generateRow($index, $value = null, $prefixName = true)
649
654
650
655
public function createPhysicalRecord ()
651
656
{
652
- $ create = Injector::inst ()->create ($ this ->value ->dataClass ());
657
+ if ($ this ->manyFieldDataClass ) {
658
+ $ create = Injector::inst ()->create ($ this ->manyFieldDataClass );
659
+ } else {
660
+ $ create = Injector::inst ()->create ($ this ->value ->dataClass ());
661
+ }
662
+
653
663
$ create ->write ();
654
664
655
- $ this ->value ->add ($ create );
665
+ if ($ this ->value instanceof SS_List) {
666
+ $ this ->value ->add ($ create );
667
+ }
656
668
657
669
return $ create ;
658
670
}
@@ -675,6 +687,7 @@ public function updateRelation(DataObjectInterface $record, $delete = true)
675
687
}
676
688
677
689
$ existing = $ record ->{$ this ->name }();
690
+
678
691
// if no value then we should clear everything out
679
692
if (!$ this ->value && $ this ->canRemove ) {
680
693
if ($ delete ) {
@@ -713,6 +726,8 @@ public function updateRelation(DataObjectInterface $record, $delete = true)
713
726
}
714
727
}
715
728
729
+ $ updatedData = [];
730
+
716
731
foreach ($ this ->value as $ col => $ values ) {
717
732
if ($ col == 'ID ' ) {
718
733
continue ;
0 commit comments