Skip to content

Instantly share code, notes, and snippets.

@davidwebca
Created March 12, 2025 01:48
Show Gist options
  • Save davidwebca/e45a73ba5d7f16c9458aa1881d1a5eae to your computer and use it in GitHub Desktop.
Save davidwebca/e45a73ba5d7f16c9458aa1881d1a5eae to your computer and use it in GitHub Desktop.
Migrating Craft CMS 5 matrix entries to their own section
<?php
/**
* https://github.com/craftcms/cms/discussions/16774#discussioncomment-12357111
*
**/
use craft\elements\Entry;
use craft\helpers\Db;
$entries = Entry::find()->field('myMatrixField');
$section = Craft::$app->entries->getSectionByHandle('mySection');
foreach (Db::each($entries) as $entry) {
Craft::$app->elements->duplicateElement($entry, [
'fieldId' => null,
'owner' => null,
'primaryOwner' => null,
'sectionId' => $section->id,
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment