Answers.Body: <p>You're getting <code>null</code> back simply because the <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/RelationsService.php#L29" rel="nofollow"><code>saveRelations()</code> method</a> doesn't actually return anything.</p>
  
  <p>If you need to return <code>true</code> from <code>addCategoryRelation</code> if the relations are successfully saved, or possibly an error message (or <code>false</code>) if they aren't, you can consider wrapping the call to <code>saveRelations()</code> in a try/catch block – something like this should work:</p>
  
  <pre><code>public function addCategoryRelation(CategoryModel $category, EntryModel $entry)
  {
      ...
      if (in_array($entry-&gt;id, array_values($relationsIds))) {
          return [
              'error' =&gt; 'Entry already saved.',
          ];
      }
      try {
          $relationsIds[] = $entry-&gt;id;
          return craft()-&gt;relations-&gt;saveRelations($field, $category, $relationsIds);
      } catch (\Exception $e) {
          // Something went wrong – return `false` or an error message, log the exception?
          return false;
      }
      return true;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-04T19:46:11.573Z
Answers.LastActivityDate: 2016-08-04T19:46:11.573Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I'm using the <code>saveRelations</code> method in the <code>RelationsService</code> to save entry relationships in a plugin I'm working on. Everything is working as expected, however, <code>saveRelations</code> is returning <code>null</code> on success, which seems odd.</p>
  
  <p>Here is a portion of the code I'm working with:</p>
  
  <pre><code>public function addCategoryRelation(CategoryModel $category, EntryModel $entry)
  {
      $field        = $craft()-&gt;fields-&gt;getFieldByHandle('handle');
      $handle       = $field-&gt;handle;
      $relations    = $category-&gt;$handle;
      $relationsIds = [];
  
      /**
       * Get the ids for the relations.
       */
      foreach ($relations as $entry) {
          $relationsIds[] = $entry-&gt;id;
      }
  
      if (!in_array($entry-&gt;id, array_values($relationsIds))) {
          $relationsIds[] = $entry-&gt;id;
          return craft()-&gt;relations-&gt;saveRelations($field, $category, $relationsIds);
      } else {
          return [
              'error' =&gt; 'Entry already saved.',
          ];
      }
  }
  </code></pre>
  
Tags: <plugin-development><relations>
Title: `saveRelations` returning `null`
ThreadText: # `saveRelations` returning `null`
  
  (Asked by: username_0 on 2016-08-04)
  
  I'm using the `saveRelations` method in the `RelationsService` to save entry relationships in a plugin I'm working on. Everything is working as expected, however, `saveRelations` is returning `null` on success, which seems odd.
  
  Here is a portion of the code I'm working with:
  
  ```
  public function addCategoryRelation(CategoryModel $category, EntryModel $entry)
  {
      $field        = $craft()->fields->getFieldByHandle('handle');
      $handle       = $field->handle;
      $relations    = $category->$handle;
      $relationsIds = [];
  
      /**
       * Get the ids for the relations.
       */
      foreach ($relations as $entry) {
          $relationsIds[] = $entry->id;
      }
  
      if (!in_array($entry->id, array_values($relationsIds))) {
          $relationsIds[] = $entry->id;
          return craft()->relations->saveRelations($field, $category, $relationsIds);
      } else {
          return [
              'error' => 'Entry already saved.',
          ];
      }
  }
  
  ```
  
  **Tags:** plugin-development, relations
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-04. Score: 1 | Accepted answer
  
  You're getting `null` back simply because the [`saveRelations()` method](https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/RelationsService.php#L29) doesn't actually return anything.
  
  If you need to return `true` from `addCategoryRelation` if the relations are successfully saved, or possibly an error message (or `false`) if they aren't, you can consider wrapping the call to `saveRelations()` in a try/catch block – something like this should work:
  
  ```
  public function addCategoryRelation(CategoryModel $category, EntryModel $entry)
  {
      ...
      if (in_array($entry->id, array_values($relationsIds))) {
          return [
              'error' => 'Entry already saved.',
          ];
      }
      try {
          $relationsIds[] = $entry->id;
          return craft()->relations->saveRelations($field, $category, $relationsIds);
      } catch (\Exception $e) {
          // Something went wrong – return `false` or an error message, log the exception?
          return false;
      }
      return true;
  }
  
  ```
  
  
  ---
  
  