Answers.Body: <p>The blocks in a Matrix field are all elements, with their owner ID set to the corresponding entry. This is how you could copy them (untested):</p>
  
  <pre><code>use craft\elements\MatrixBlock;
  
  foreach ($entryA-&gt;matrixFieldHandle-&gt;all() as $matrixBlockA) {
      $matrixBlockB = new MatrixBlock();
      $matrixBlockB-&gt;fieldId = $matrixBlockA-&gt;fieldId;
      $matrixBlockB-&gt;typeId = $matrixBlockA-&gt;typeId;
      $matrixBlockB-&gt;ownerId = $entryB-&gt;id;
      $matrixBlockB-&gt;siteId = $entryB-&gt;siteId;
      $matrixBlockB-&gt;enabled = $matrixBlockA-&gt;enabled;
      $matrixBlockB-&gt;sortOrder = $matrixBlockA-&gt;sortOrder;
      $matrixBlockB-&gt;setFieldValues($matrixBlockA-&gt;getFieldValues());
  
      Craft::$app-&gt;getElements()-&gt;saveElement($matrixBlockB);
  }
  </code></pre>
  
  <p>Things can get tricky if you have multiple sites and the entries do or do not get propagated (etc), but this is how it works in a nutshell.</p>
  
  <p>For the above code to work, you have to first save Entry B, to get an entry ID for it. So, create Entry B, fill 'normal' fields, save Entry B, create Matrix B, save Matrix B.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-29T13:40:22.267Z
Answers.LastActivityDate: 2018-11-29T13:40:22.267Z
Answers.UserDisplayName: Paul
Body: <p>I've got <code>$entryA</code> with a Matrix field.</p>
  
  <p>I want to programatically create a new <code>$entryB</code> and copy the Matrix field from <code>$entryA</code> to <code>$entryB</code>.  How do I go about this?</p>
  
Tags: <craft3><matrix>
Title: How to copy Matrix from one entry to another in Craft 3
ThreadText: # How to copy Matrix from one entry to another in Craft 3
  
  (Asked by: username_0 on 2018-11-29)
  
  I've got `$entryA` with a Matrix field.
  
  I want to programatically create a new `$entryB` and copy the Matrix field from `$entryA` to `$entryB`. How do I go about this?
  
  **Tags:** craft3, matrix
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-11-29. Score: 2 | Accepted answer
  
  The blocks in a Matrix field are all elements, with their owner ID set to the corresponding entry. This is how you could copy them (untested):
  
  ```
  use craft\elements\MatrixBlock;
  
  foreach ($entryA->matrixFieldHandle->all() as $matrixBlockA) {
      $matrixBlockB = new MatrixBlock();
      $matrixBlockB->fieldId = $matrixBlockA->fieldId;
      $matrixBlockB->typeId = $matrixBlockA->typeId;
      $matrixBlockB->ownerId = $entryB->id;
      $matrixBlockB->siteId = $entryB->siteId;
      $matrixBlockB->enabled = $matrixBlockA->enabled;
      $matrixBlockB->sortOrder = $matrixBlockA->sortOrder;
      $matrixBlockB->setFieldValues($matrixBlockA->getFieldValues());
  
      Craft::$app->getElements()->saveElement($matrixBlockB);
  }
  
  ```
  
  Things can get tricky if you have multiple sites and the entries do or do not get propagated (etc), but this is how it works in a nutshell.
  
  For the above code to work, you have to first save Entry B, to get an entry ID for it. So, create Entry B, fill 'normal' fields, save Entry B, create Matrix B, save Matrix B.
  
  
  ---
  
  