Answers.Body: <p>Relations fields can store multiple related values (even if you only have one item).  Perhaps try to pass an array to the 'quiz' value: </p>
  
  <pre><code>'quiz' =&gt; array($data['itemId'])
  </code></pre>
  
  <p>Alternatively, if you are submitting all of your content, in the correct format, you can get rid of your <code>$entry-&gt;getContent()-&gt;setAttributes</code> line and use the <code>setContentFromPost</code> method.</p>
  
  <p>All you need to pass to the <code>setContentFromPost</code> method is the name of the custom content fields array you are submitting to the content table.  For example, if your fields above were structured as follows in your front-end form template (this is a bit simplified for your complex field types like relations, but as long as your submitting the right values this should work):</p>
  
  <pre><code>&lt;input type="text" name="title" value="My New Entry"&gt; 
  &lt;input type="text" name="fields[score]" value="101"&gt;
  &lt;input type="text" name="fields[quiz][]" value="101"&gt;
  </code></pre>
  
  <p>Note that in the input field name of <code>quiz</code> relations field we are passing an array: <code>fields[quiz][]</code>.</p>
  
  <p>You can then receive your submitted data and hand it off to the ContentModel tied to your EntryModel. Your title is handled slightly differently than your custom content as the title field is a default attribute of the ContentModel:</p>
  
  <pre><code>$entry-&gt;getContent()-&gt;title = craft()-&gt;request-&gt;getPost('title');
  </code></pre>
  
  <p>And your array of submitted custom content can be handed off silently to the <code>setContentFromPost</code> field by just passing it the name of all of the fields.</p>
  
  <pre><code>$entry-&gt;setContentFromPost('fields');
  </code></pre>
  
  <p>You don't even need to capture your post data using something like <code>craft()-&gt;request-&gt;getPost('fields')</code>, just passing the 'fields' value to the function does all the heavy lifting for you.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-19T06:26:28.263Z
Answers.LastActivityDate: 2014-07-19T06:26:28.263Z
Answers.UserDisplayName: Ben Parizek
Body: <p>In my plugin, how do I save a relationship when creating a new entry? The example below works for all but the "quiz" relationship.</p>
  
  <pre class="lang-php prettyprint-override"><code>$triviaSet = craft()-&gt;entries-&gt;getEntryById($data['itemId']);
  $entry = new EntryModel();
  $entry-&gt;sectionId   = 3
  $entry-&gt;typeId     = 2
  $entry-&gt;authorId   = 1;
  $entry-&gt;enabled    = true;
  $entry-&gt;getContent()-&gt;setAttributes(array(
      'title' =&gt; 'My New Entry',
      'score' =&gt; $data['score'],
      'quiz' =&gt; $triviaSet
  ));
  $success = craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
Tags: <plugin-development><entries><relations>
Title: In a plugin, how do I save an entry with a related entry
ThreadText: # In a plugin, how do I save an entry with a related entry
  
  (Asked by: username_0 on 2014-07-18)
  
  In my plugin, how do I save a relationship when creating a new entry? The example below works for all but the "quiz" relationship.
  
  ```php
  $triviaSet = craft()->entries->getEntryById($data['itemId']);
  $entry = new EntryModel();
  $entry->sectionId   = 3
  $entry->typeId     = 2
  $entry->authorId   = 1;
  $entry->enabled    = true;
  $entry->getContent()->setAttributes(array(
      'title' => 'My New Entry',
      'score' => $data['score'],
      'quiz' => $triviaSet
  ));
  $success = craft()->entries->saveEntry($entry);
  
  ```
  
  **Tags:** plugin-development, entries, relations
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-07-19. Score: 6 | Accepted answer
  
  Relations fields can store multiple related values (even if you only have one item). Perhaps try to pass an array to the 'quiz' value:
  
  ```
  'quiz' => array($data['itemId'])
  
  ```
  
  Alternatively, if you are submitting all of your content, in the correct format, you can get rid of your `$entry->getContent()->setAttributes` line and use the `setContentFromPost` method.
  
  All you need to pass to the `setContentFromPost` method is the name of the custom content fields array you are submitting to the content table. For example, if your fields above were structured as follows in your front-end form template (this is a bit simplified for your complex field types like relations, but as long as your submitting the right values this should work):
  
  ```
  <input type="text" name="title" value="My New Entry"> 
  <input type="text" name="fields[score]" value="101">
  <input type="text" name="fields[quiz][]" value="101">
  
  ```
  
  Note that in the input field name of `quiz` relations field we are passing an array: `fields[quiz][]`.
  
  You can then receive your submitted data and hand it off to the ContentModel tied to your EntryModel. Your title is handled slightly differently than your custom content as the title field is a default attribute of the ContentModel:
  
  ```
  $entry->getContent()->title = craft()->request->getPost('title');
  
  ```
  
  And your array of submitted custom content can be handed off silently to the `setContentFromPost` field by just passing it the name of all of the fields.
  
  ```
  $entry->setContentFromPost('fields');
  
  ```
  
  You don't even need to capture your post data using something like `craft()->request->getPost('fields')`, just passing the 'fields' value to the function does all the heavy lifting for you.
  
  
  ---
  
  