Answers.Body: <p>ok just found out the answer to the second method. I was using the incorrect typeId apparently. I wasn't able to find this in any of the error logs, so this was just luck.</p>
  
  <p>To make sure you get the correct typeId do so in this way:</p>
  
  <pre><code>$matrix = craft()-&gt;fields-&gt;getFieldByHandle("fieldHandle"); //look in craft_fields table
  $matrixBlocks = craft()-&gt;matrix-&gt;getBlockTypesByFieldId($matrix-&gt;id);
  </code></pre>
  
  <p>You can iterate through $matrixBlocks and look at each blocks id but in my case I knew there was only one block so I just accessed the id like this</p>
  
  <pre><code>$typeId = $matrixBlocks[0]['id'];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-26T18:07:57.443Z
Answers.LastActivityDate: 2015-06-29T19:38:09.133Z
Answers.LastEditDate: 2015-06-29T19:38:09.133Z
Answers.UserDisplayName: Panda4Man
Body: <p>I have been trying two separate methods to accomplish and both keep resulting in the same errors no matter what I try.</p>
  
  <p>The entry I am saving has a few fields the last of which is a matrix block (I am able to save the entry fine when not trying to save the matrix field to it.). This matrix block has two entry field types and the last field is a table field. I do not need to save actual data to these fields; it's fine that they are null/empty. I made sure that in the matrix block settings for these fields are not marked as required.</p>
  
  <p>I am testing this via ajax calls in jQuery.</p>
  
  <p>First Method (described <a href="https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin">here</a>):
  Craft sends back an error stating that...</p>
  
  <pre><code>Argument 1 passed to Craft\MatrixService::validateBlock() must be an instance
  of Craft\MatrixBlockModel, array given, called in 
  /path/to/project/app/fieldtypes/MatrixFieldType.php 
  on line 393 and defined
  </code></pre>
  
  <p>It appears to me that craft is expecting a matrix block model not an array. But even Brandon Kelly shows an example <a href="https://gist.github.com/brandonkelly/8149062" rel="nofollow noreferrer">here</a> of this method working...so what am I doing wrong..?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>private function _buildEntry ($college, $now, &amp;$data) {
      $entry = new EntryModel();
      $matrixData = array();
      $entry-&gt;sectionId = 4;
      $entry-&gt;authorId = 1;
      $entry-&gt;typeId = 1;
      $entry-&gt;enabled = 1;
      $entry-&gt;getContent()-&gt;title = $college-&gt;collegeName." ".$now;
      $entry-&gt;getContent()-&gt;timeEntryStartDate = $now;
      $entry-&gt;getContent()-&gt;timeEntryCollege = array($college-&gt;id);
  
      //set up the matrix data
      $matrixData['new1'] = array(
          'type'      =&gt; 'weekBlock',
          'enabled'   =&gt; true
      );
  
      $entry-&gt;getContent()-&gt;timeEntryWeek = $matrixData;
  
      //save the new entry
      $success = craft()-&gt;entries-&gt;saveEntry($entry);
  
      if(!$success){
          Craft::log('Could not save the entry "'.$entry-&gt;title.'"', LogLevel::Error);
      } else {
          Craft::log('Saved the entry! '.$entry-&gt;title.'"', LogLevel::Error);
      }
  }
  </code></pre>
  
  <p>Second Method (described <a href="https://gist.github.com/bwlng/5c0bdeff62fabe247c87" rel="nofollow noreferrer">here</a>): Craft sends back an error stating that...</p>
  
  <pre><code>Trying to get property of non-object
  </code></pre>
  
  <p>My code for this second part which I call after craft successfully saves the entry (which works fine)</p>
  
  <pre><code>public function actionTest($id = 3) {
  
      $block = new MatrixBlockModel();
  
      $block-&gt;fieldId    = 20; // Matrix field's ID
      $block-&gt;ownerId    = 178; // ID of entry the block should be added to
      $block-&gt;typeId     = 6;
      $block-&gt;getContent()-&gt;setAttributes(array(
          'timeEntryHouse' =&gt; array(183),
          'timeEntryEmployee' =&gt; array(1),
          'timeEntryDays' =&gt; null
      ));
  
      $success = craft()-&gt;matrix-&gt;saveBlock($block, false);
      if (!$success)
      {
          Craft::log('Couldn’t save the entry "'.$id.'"', LogLevel::Error);
      } else {
          $this-&gt;returnJson('worked');
      }
  }
  </code></pre>
  
Tags: <plugin-development><matrix><entry>
Title: Save matrix block to new entry in plugin
ThreadText: # Save matrix block to new entry in plugin
  
  (Asked by: username_0 on 2015-06-26)
  
  I have been trying two separate methods to accomplish and both keep resulting in the same errors no matter what I try.
  
  The entry I am saving has a few fields the last of which is a matrix block (I am able to save the entry fine when not trying to save the matrix field to it.). This matrix block has two entry field types and the last field is a table field. I do not need to save actual data to these fields; it's fine that they are null/empty. I made sure that in the matrix block settings for these fields are not marked as required.
  
  I am testing this via ajax calls in jQuery.
  
  First Method (described [here](https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin)):
  Craft sends back an error stating that...
  
  ```
  Argument 1 passed to Craft\MatrixService::validateBlock() must be an instance
  of Craft\MatrixBlockModel, array given, called in 
  /path/to/project/app/fieldtypes/MatrixFieldType.php 
  on line 393 and defined
  
  ```
  
  It appears to me that craft is expecting a matrix block model not an array. But even Brandon Kelly shows an example [here](https://gist.github.com/brandonkelly/8149062) of this method working...so what am I doing wrong..?
  
  Here's my code:
  
  ```
  private function _buildEntry ($college, $now, &$data) {
      $entry = new EntryModel();
      $matrixData = array();
      $entry->sectionId = 4;
      $entry->authorId = 1;
      $entry->typeId = 1;
      $entry->enabled = 1;
      $entry->getContent()->title = $college->collegeName." ".$now;
      $entry->getContent()->timeEntryStartDate = $now;
      $entry->getContent()->timeEntryCollege = array($college->id);
  
      //set up the matrix data
      $matrixData['new1'] = array(
          'type'      => 'weekBlock',
          'enabled'   => true
      );
  
      $entry->getContent()->timeEntryWeek = $matrixData;
  
      //save the new entry
      $success = craft()->entries->saveEntry($entry);
  
      if(!$success){
          Craft::log('Could not save the entry "'.$entry->title.'"', LogLevel::Error);
      } else {
          Craft::log('Saved the entry! '.$entry->title.'"', LogLevel::Error);
      }
  }
  
  ```
  
  Second Method (described [here](https://gist.github.com/bwlng/5c0bdeff62fabe247c87)): Craft sends back an error stating that...
  
  ```
  Trying to get property of non-object
  
  ```
  
  My code for this second part which I call after craft successfully saves the entry (which works fine)
  
  ```
  public function actionTest($id = 3) {
  
      $block = new MatrixBlockModel();
  
      $block->fieldId    = 20; // Matrix field's ID
      $block->ownerId    = 178; // ID of entry the block should be added to
      $block->typeId     = 6;
      $block->getContent()->setAttributes(array(
          'timeEntryHouse' => array(183),
          'timeEntryEmployee' => array(1),
          'timeEntryDays' => null
      ));
  
      $success = craft()->matrix->saveBlock($block, false);
      if (!$success)
      {
          Craft::log('Couldn’t save the entry "'.$id.'"', LogLevel::Error);
      } else {
          $this->returnJson('worked');
      }
  }
  
  ```
  
  **Tags:** plugin-development, matrix, entry
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-06-26. Score: 3
  
  ok just found out the answer to the second method. I was using the incorrect typeId apparently. I wasn't able to find this in any of the error logs, so this was just luck.
  
  To make sure you get the correct typeId do so in this way:
  
  ```
  $matrix = craft()->fields->getFieldByHandle("fieldHandle"); //look in craft_fields table
  $matrixBlocks = craft()->matrix->getBlockTypesByFieldId($matrix->id);
  
  ```
  
  You can iterate through $matrixBlocks and look at each blocks id but in my case I knew there was only one block so I just accessed the id like this
  
  ```
  $typeId = $matrixBlocks[0]['id'];
  
  ```
  
  
  ---
  
  