Skip to content

Commit

Permalink
Merge pull request #31 from enzorrr/keep-unmodified
Browse files Browse the repository at this point in the history
just save relations that were added instead of re-saving all relations
  • Loading branch information
boboldehampsink authored Dec 15, 2021
2 parents 86b9880 + f502312 commit 4b67216
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.3.1 - 2021-12-14
### Changed
- Omit saving of relations that were not modified

## 1.3.0 - 2021-08-25
### Changed
- Added Craft 3.7 compatibility (thanks to @brandonkelly)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.3.0",
"version": "1.3.1",
"name": "robuust/craft-reverserelations",
"description": "Reverse Relations for Craft 3",
"type": "craft-plugin",
Expand Down
9 changes: 6 additions & 3 deletions src/fields/ReverseRelationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ public function afterElementSave(ElementInterface $element, bool $isNew)
// Get sources
$sources = (clone $element->getFieldValue($this->handle))->anyStatus()->all();

// Find out which ones to add
$add = array_diff($sources, $this->oldSources);

// Find out which ones to delete
$delete = array_diff($this->oldSources, $sources);

// Loop through sources
/** @var ElementInterface $source */
foreach ($sources as $source) {
// Loop through all items that need to be added
/** @var ElementInterface $add */
foreach ($add as $source) {
$target = (clone $source->getFieldValue($field->handle))->anyStatus();

// Set this element on that element
Expand Down

0 comments on commit 4b67216

Please sign in to comment.