Closed sqp-alpaca closed 3 weeks ago
Hi, thanks for getting in touch! Which version did you update from?
I updated from 4.10.8 to 4.11.0
The reason it’s breaking is because you aren’t unsetting the entry’s contentId
value, which stores the ID of the entry/site’s row in the content
table. So initially when saveElement()
is called, the original content row will get reassigned to the new site here:
When saveElement()
goes to propagate the entry to its other sites, it won’t be able to fetch the entry in the original site anymore (because the data is now incomplete for it, so not all of the join
s won’t resolve), so it will assume the entry doesn’t exist at all for the original site and try to recreate it there—resulting in the SQL error because the entry does partially exist there still (in the elements_sites
table).
@i-just found that this change is what ended up causing this to break in 4.11, but I’m surprised it worked to begin with.
The proper way to add an entry to additional sites is via its enabledForSites
property:
use craft\helpers\ElementHelper;
$entry = Craft::$app->getEntries()->getEntryById(24);
// see if we need to add the entry to any sites
$addSites = array_values(array_filter($entry->getSupportedSites(), fn($site) => $site['propagate'] === false));
if ($addSites) {
// get the current site statuses and merge in the missing ones
$siteStatuses = ElementHelper::siteStatusesForElement($entry);
foreach ($addSites as $site) {
$siteStatuses[$site['siteId']] = true;
}
$entry->setEnabledForSite($siteStatuses);
// save the entry
Craft::$app->getElements()->saveElement($entry);
}
What happened?
Description
I am experiencing an issue with the propagation method "Let each entry choose which sites it should be saved to" in a section of my Craft CMS setup. Previously, I implemented a feature in the Control Panel that allows users to save an entry across all sites by clicking a button. This is achieved by running a job in the ProjectExtention that saves the entry to each site. Below is the code I use:
However, since updating to Craft CMS 4.11.0, this code fails during the save operation within the try-catch block, throwing the following error:
It appears that I can no longer directly overwrite the siteId on an existing entry.
Temporary Fix
I have found a workaround by creating a new copy of the entry and setting the siteId on this copy. Since I’m reusing the same id and uid, it’s essentially the same entry from a technical perspective. Here’s the adjusted code:
Question
Given that technically this is the same entry (since the id and uid remain unchanged), why is it necessary to overwrite all these additional attributes when all I want to change is the siteId? Is there an updated method or best practice in Craft CMS 4.11.0 for changing the siteId of an existing entry, or are there any new checks in place that might be preventing my code from saving entries across different sites? Any guidance on how to address this issue would be greatly appreciated.
Thank you for your support!
Craft CMS version
4.11.0
PHP version
8.2
Operating system and version
Linux (Cyon-Hosting)
Database type and version
10.6.14-MariaDB-cll-lve-log - MariaDB Server
Image driver and version
No response
Installed plugins and versions