Answers.Body: <p>Possibly a duplicate of <a href="https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin/1218">this one here</a> but hey, here is where I think you are going wrong:</p>
  
  <p><strong>Change this part:</strong></p>
  
  <pre><code>$block-&gt;getContent()-&gt;setAttributes(array(
    'contentRss' =&gt; array($entry-&gt;id)
  ));
  </code></pre>
  
  <p><strong>To this:</strong></p>
  
  <pre><code>$block-&gt;getContent()-&gt;contentRss = array($entry-&gt;id);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-29T05:26:59.537Z
Answers.Text: No worries, did it work?
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T05:27:17.193Z
Answers.Text: Excellent! In which case could you mark this answer as correct to better help out other people?
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-28T08:03:30.673Z
Answers.LastActivityDate: 2014-07-28T08:03:30.673Z
Answers.LastEditDate: 2017-04-13T12:51:00.253Z
Answers.UserDisplayName: Josh Angell
Body: <p>I'm trying to add entries to a Matrix block with one field type of 'entry' using the following:</p>
  
  <pre><code>$block = new MatrixBlockModel();
  $block-&gt;fieldId    = 9;             // Matrix field's ID
  $block-&gt;ownerId    = $collectionID; // ID of entry the block should be added to
  $block-&gt;typeId     = 2;             // ID of block type
  $block-&gt;getContent()-&gt;setAttributes(array(
      'contentRss' =&gt; array($entry-&gt;id)
  ));
  
  $success = craft()-&gt;matrix-&gt;saveBlock($block);
  </code></pre>
  
  <p>The code saves the new block and entry, but when I view the entry it has not linked the entry. I notice in the database tables it has not created an entry in the <code>craft_relations</code> table.</p>
  
  <p>Where am I going wrong? :)</p>
  
Tags: <plugin-development>
Title: Saving related entry in a Matrix block programmaticially
ThreadText: # Saving related entry in a Matrix block programmaticially
  
  (Asked by: username_0 on 2014-07-28)
  
  I'm trying to add entries to a Matrix block with one field type of 'entry' using the following:
  
  ```
  $block = new MatrixBlockModel();
  $block->fieldId    = 9;             // Matrix field's ID
  $block->ownerId    = $collectionID; // ID of entry the block should be added to
  $block->typeId     = 2;             // ID of block type
  $block->getContent()->setAttributes(array(
      'contentRss' => array($entry->id)
  ));
  
  $success = craft()->matrix->saveBlock($block);
  
  ```
  
  The code saves the new block and entry, but when I view the entry it has not linked the entry. I notice in the database tables it has not created an entry in the `craft_relations` table.
  
  Where am I going wrong? :)
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-07-28. Score: 7 | Accepted answer
  
  Possibly a duplicate of [this one here](https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin/1218) but hey, here is where I think you are going wrong:
  
  **Change this part:**
  
  ```
  $block->getContent()->setAttributes(array(
    'contentRss' => array($entry->id)
  ));
  
  ```
  
  **To this:**
  
  ```
  $block->getContent()->contentRss = array($entry->id);
  
  ```
  
  > **Comments:**
  > **username_1**: No worries, did it work?  
  > **username_1**: Excellent! In which case could you mark this answer as correct to better help out other people?  
  
  ---
  
  