|
10 | 10 | use SilverStripe\Control\Director;
|
11 | 11 | use SilverStripe\Control\Controller;
|
12 | 12 | use SilverStripe\Forms\FormField;
|
| 13 | +use SilverStripe\Security\SecurityToken; |
| 14 | +use SilverStripe\Core\Injector\Injector; |
13 | 15 | use SilverStripe\Control\HTTPResponse;
|
14 | 16 |
|
15 | 17 | class ManyField extends CompositeField
|
@@ -258,6 +260,14 @@ public function createNewRecord()
|
258 | 260 | return $response;
|
259 | 261 | }
|
260 | 262 |
|
| 263 | + public function AddLink() |
| 264 | + { |
| 265 | + return Controller::join_links( |
| 266 | + $this->AbsoluteLink('createNewRecord'), |
| 267 | + '?SecurityID='. SecurityToken::inst()->getValue() |
| 268 | + ); |
| 269 | + } |
| 270 | + |
261 | 271 | /**
|
262 | 272 | * Return the list of fields. We'll create a row for each of the values if
|
263 | 273 | * they exist otherwise we'll only return
|
@@ -350,36 +360,42 @@ public function updateRelation(DataObjectInterface $record, $delete = true)
|
350 | 360 | $existing->removeById($row->ID);
|
351 | 361 | }
|
352 | 362 | }
|
| 363 | + } |
353 | 364 |
|
| 365 | + if (isset($this->value['ID'])) { |
354 | 366 | foreach ($this->value['ID'] as $key => $id) {
|
355 | 367 | if ($id) {
|
356 | 368 | $idKeyMap[$key] = $id;
|
357 | 369 | }
|
358 | 370 | }
|
359 |
| - |
360 |
| - foreach ($this->value as $col => $values) { |
361 |
| - if ($col == 'ID') { |
362 |
| - continue; |
363 |
| - } |
364 |
| - |
365 |
| - foreach ($values as $key => $value) { |
366 |
| - if (!isset($updatedData[$key])) { |
367 |
| - $updatedData[$key] = []; |
368 |
| - } |
| 371 | + } |
| 372 | + |
| 373 | + foreach ($this->value as $col => $values) { |
| 374 | + if ($col == 'ID') { |
| 375 | + continue; |
| 376 | + } |
369 | 377 |
|
370 |
| - $updatedData[$key][$col] = $value; |
| 378 | + foreach ($values as $key => $value) { |
| 379 | + if (!isset($updatedData[$key])) { |
| 380 | + $updatedData[$key] = []; |
371 | 381 | }
|
| 382 | + |
| 383 | + $updatedData[$key][$col] = $value; |
372 | 384 | }
|
| 385 | + } |
373 | 386 |
|
374 |
| - foreach ($updatedData as $key => $data) { |
375 |
| - if (isset($idKeyMap[$key])) { |
376 |
| - $existing->find('ID', $idKeyMap[$key])->update($row); |
377 |
| - } else { |
378 |
| - $create = Injector::inst()->create($existing->dataClass()); |
379 |
| - $create->update($data); |
380 |
| - $create->write(); |
381 |
| - $existing->add($create); |
382 |
| - } |
| 387 | + foreach ($updatedData as $key => $data) { |
| 388 | + if (isset($idKeyMap[$key])) { |
| 389 | + $record = $existing->find('ID', $idKeyMap[$key]); |
| 390 | + |
| 391 | + $record->update($data); |
| 392 | + $record->write(); |
| 393 | + } else { |
| 394 | + $create = Injector::inst()->create($existing->dataClass()); |
| 395 | + $create->update($data); |
| 396 | + |
| 397 | + $create->write(); |
| 398 | + $existing->add($create); |
383 | 399 | }
|
384 | 400 | }
|
385 | 401 |
|
|
0 commit comments