@bernhard Here's a function that solves the original problem of how to MOVE (not copy!) repeater items from one page to another, which preserves IDs.
I tested it and I believe I accounted for everything, but I recommend testing it more before using it in production.
// move the repeater items from fromPage to toPage
// the same repeater field must be assigned to both pages
// note: fromPage and toPage can be repeater page items as well since they are technically pages
function moveRepeaterItems(string $fieldName, Page|RepeaterPage $fromPage, Page|RepeaterPage $toPage): void {
// checks
if(!wire('fields')->get($fieldName)) {
throw new WireException("Field '$fieldName' does not exist.");
}
if(!$fromPage->id) {
throw new WireException("From page does not exist.");
}
if(!$toPage->id) {
throw new WireException("To page does not exist.");
}
if(!$fromPage->hasField($fieldName)) {
throw new WireException("From page does not have field '$fieldName'.");
}
if(!$toPage->hasField($fieldName)) {
throw new WireException("To page does not have field '$fieldName'.");
}
if($toPage->get($fieldName)->count('include=all,check_access=0')) {
throw new WireException("To page already has items in field '$fieldName'.");
}
// store the parent_id
$parent_id = wire('database')->query("SELECT parent_id FROM field_{$fieldName} WHERE pages_id = '{$fromPage->id}'")->fetchColumn();
// delete potential (and likely) existing toPage data placeholder
// prevents this error: Integrity constraint violation: 1062 Duplicate entry '1491109' for key 'PRIMARY' in /wire/core/WireDatabasePDO.php:783
// remember, this will be empty since we checked above that there are no items in the toPage field
wire('database')->query("DELETE FROM `field_{$fieldName}` WHERE `pages_id` = '{$toPage->id}'");
// update the record in table 'field_$field' where pages_id=$fromPage->id and change the pages_id to $toPage->id
wire('database')->query("UPDATE `field_{$fieldName}` SET `pages_id` = '{$toPage->id}' WHERE `pages_id` = '{$fromPage->id}'");
// update the record in table 'pages' where id=$parent_id: change name from 'for-page-{$fromPage->id}' to 'for-page-{$toPage->id}'
wire('database')->query("UPDATE `pages` SET `name` = 'for-page-{$toPage->id}' WHERE `id` = '{$parent_id}'");
}
// example
moveRepeaterItems(
fieldName: 'order_line_items',
fromPage: $pages->get("/orders/foo/"),
toPage: $pages->get("/orders/bar/")
);