That works perfectly. I had a go at adding the oldResource children ’published’ statuses to an array and then using that to set the new children, but I can’t figure out why that isn’t working. Here’s the code I’m using:
<?php
$pubStatus = $oldResource->get('published');
// Publish top-level resource
$newResource->set('published', $pubStatus);
$newResource->save();
/*function iterateChildren($children,$pubStatus) {
foreach ($children as $child) {
$child->set('published', $pubStatus);
$child->save();
$childChildren = $child->getMany('Children');
iterateChildren($childChildren,$pubStatus);
}
}*/
function iterateChildren($children,$pubStatuses,$counter) {
foreach ($children as $child) {
// Get pubStatus for current child
$pubStatus = $pubStatuses[$counter];
// Set child to its pubStatus
$child->set('published', $pubStatus);
$child->save();
$counter++;
// Find children of child
$childChildren = $child->getMany('Children');
// Recursively iterate through children and set counter to eventual value
$counter = iterateChildren($childChildren,$pubStatuses,$counter);
}
return $counter;
}
function getChildrenPubStatus($oldResourceChildren,$childrenPubStatus) {
// Loop through each oldResource child
foreach ($oldResourceChildren as $oldResourceChild) {
// Append oldResource child pubStatus to childrenPubStatus array
$childrenPubStatus[] = $oldResourceChild->get('published');
// Get children of child
$oldResourceChildChildren = $oldResourceChild->getMany('Children');
// Loop recursively
getChildrenPubStatus($oldResourceChildChildren,$childrenPubStatus);
return $childrenPubStatus;
}
}
// Get children as array
$children = $newResource->getMany('Children');
// Set children to published
//iterateChildren($children,$pubStatus);
// Get children of oldResource
$oldResourceChildren = $oldResource->getMany('Children');
$childrenPubStatus = array();
// Put pubStatuses into an array
$childrenPubStatus = getChildrenPubStatus($oldResourceChildren,$childrenPubStatus);
// Recursively loop through children, setting them to original pubStatus value
iterateChildren($children,$childrenPubStatuses,0);
If anyone needs the working version that sets everything to the published status of the top-level oldResource, this works:
<?php
$pubStatus = $oldResource->get('published');
// Publish top-level resource
$newResource->set('published', $pubStatus);
$newResource->save();
function iterateChildren($children,$pubStatus) {
foreach ($children as $child) {
$child->set('published', $pubStatus);
$child->save();
$childChildren = $child->getMany('Children');
iterateChildren($childChildren,$pubStatus);
}
}
// Get children as array
$children = $newResource->getMany('Children');
// Set children to published
iterateChildren($children,$pubStatus);
Thanks for all the help; I can only hope that one day I will have learnt enough PHP to be as helpful to the community as BobRay is...