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.

Revisions

  1. davidwebca created this gist Mar 12, 2025.
    20 changes: 20 additions & 0 deletions put-that-in-a-migration.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?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,
    ]);
    }