Answers.Body: <p>You can save Matrix blocks directly, without having to re-save the entry.</p>
  
  <h2>Craft 2</h2>
  
  <p>Use <a href="https://docs.craftcms.com/api/v2/craft-matrixservice.html#method-saveblock" rel="noreferrer">MatrixService::saveBlock()</a>:</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>$block = new MatrixBlockModel();
  $block-&gt;fieldId = 5;
  $block-&gt;ownerId = 100;
  $block-&gt;ownerLocale = 'en_us';
  $block-&gt;typeId = 2;
  $block-&gt;sortOrder = 10;
  
  $block-&gt;setContentFromPost(array(
      'fieldHandle' =&gt; 'value',
      // ...
  ));
  
  $success = craft()-&gt;matrix-&gt;saveBlock($block);
  </code></pre>
  
  <h2>Craft 3</h2>
  
  <p>Use <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement" rel="noreferrer">craft\services\Elements::saveElement()</a>:</p>
  
  <pre class="lang-php prettyprint-override"><code>use Craft;
  use craft\elements\MatrixBlock;
  
  $block = new MatrixBlock();
  $block-&gt;fieldId = 5;
  $block-&gt;ownerId = 100;
  $block-&gt;siteId = 1;
  $block-&gt;typeId = 2;
  $block-&gt;sortOrder = 10;
  
  $block-&gt;setFieldValues([
      'fieldHandle' =&gt; 'value',
      // ...
  ]);
  
  $success = Craft::$app-&gt;elements-&gt;saveElement($block);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-20T21:49:44.640Z
Answers.Text: @BrandonKelly how has this changed in Craft 3? I don't see a `MatrixBlockModel`.
Answers.UserDisplayName: cmal
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-21T13:41:03.383Z
Answers.Text: @cmal just updated my answer for Craft 3.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-26T01:15:34.807Z
Answers.Text: Thanks, that worked for me.
  
  Here's a gist that shows how this can be used to create a new entry if there isn't one for the block to be added to yet if anyone else needs to do this: https://gist.github.com/bwlng/5c0bdeff62fabe247c87
  
  Would resaving the entry after adding all the blocks update the search keywords?
Answers.UserDisplayName: Stephen Bowling
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-27T15:03:21.650Z
Answers.Text: @StephenBowling Yes that’s a possibility. Just updated the code example showing how you could re-save the entry's content after saving the new matrix block.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-07-25T23:13:34.337Z
Answers.LastActivityDate: 2018-11-21T20:40:46.887Z
Answers.LastEditDate: 2018-11-21T20:40:46.887Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I'm building a plugin that adds blocks to a matrix in an entry from the front end. I got it up and running by following the answer in this post: </p>
  
  <p><a href="https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin/1218#1218">How to save a matrix content of a new entry in my plugin?</a></p>
  
  <p>How do I check if the matrix already has blocks when I fetch the entry? And then, once I know if the matrix already has blocks, how do I retain the existing data while saving new blocks via the plugin?</p>
  
Tags: <plugin-development>
Title: How do I add a block to a matrix that already has content via a plugin?
ThreadText: # How do I add a block to a matrix that already has content via a plugin?
  
  (Asked by: username_0 on 2014-07-25)
  
  I'm building a plugin that adds blocks to a matrix in an entry from the front end. I got it up and running by following the answer in this post:
  
  [How to save a matrix content of a new entry in my plugin?](https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin/1218#1218)
  
  How do I check if the matrix already has blocks when I fetch the entry? And then, once I know if the matrix already has blocks, how do I retain the existing data while saving new blocks via the plugin?
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-07-25. Score: 13 | Accepted answer
  
  You can save Matrix blocks directly, without having to re-save the entry.
  
  ## Craft 2
  
  Use [MatrixService::saveBlock()](https://docs.craftcms.com/api/v2/craft-matrixservice.html#method-saveblock):
  
  ```php
  $block = new MatrixBlockModel();
  $block->fieldId = 5;
  $block->ownerId = 100;
  $block->ownerLocale = 'en_us';
  $block->typeId = 2;
  $block->sortOrder = 10;
  
  $block->setContentFromPost(array(
      'fieldHandle' => 'value',
      // ...
  ));
  
  $success = craft()->matrix->saveBlock($block);
  
  ```
  
  ## Craft 3
  
  Use [craft\services\Elements::saveElement()](https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement):
  
  ```php
  use Craft;
  use craft\elements\MatrixBlock;
  
  $block = new MatrixBlock();
  $block->fieldId = 5;
  $block->ownerId = 100;
  $block->siteId = 1;
  $block->typeId = 2;
  $block->sortOrder = 10;
  
  $block->setFieldValues([
      'fieldHandle' => 'value',
      // ...
  ]);
  
  $success = Craft::$app->elements->saveElement($block);
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, that worked for me.  Here's a gist that shows how this can be used to create a new entry if there isn't one for the block to be added to yet if anyone else needs to do this: https://gist.github.com/bwlng/5c0bdeff62fabe247c87  Would resaving the entry after adding all the blocks update the search keywords?  
  > **username_1**: @username_0 Yes that’s a possibility. Just updated the code example showing how you could re-save the entry's content after saving the new matrix block.  
  > **username_2**: @username_1 how has this changed in Craft 3? I don't see a `MatrixBlockModel`.  
  > **username_1**: @username_2 just updated my answer for Craft 3.  
  
  ---
  
  