Skip to content

[5.x]: Elements::mergeElements() doesn't update content relations #17817

@MangoMarcus

Description

@MangoMarcus

What happened?

Description

I've used \craft\services\Elements::mergeElements() but it doesn't seem to update relations properly. My outgoing asset was related to an entry (directly) and after merging, the persisting asset didn't take it's place.

I did a bit of digging and it seems that although it does update rows in the relations table, it doesn't update the content column in the elements_sites table.

It does add a FindAndReplace queue job, but this only updates reference tags.

After merging it still contains the old asset's id:

{
    // ...
    "c4e361f2-86cb-4a0d-9c11-1d948f487605": [
        135876
    ],
    // ...
}

Steps to reproduce

  1. Create an entry type with an asset field
  2. Add 2 assets
  3. Save an entry with the asset field set to one of the assets
  4. Use \craft\services\Elements::mergeElements() to merge the 2 assets - eg. in a console action

Expected behavior

The entry should now show the second asset in the CMS

Actual behavior

Asset field appears empty

Craft CMS version

Craft Pro 5.8.16

PHP version

8.3.21

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Safe mode

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions