Answers.Body: <p>To set a related category (or any other kind of element) you will want to pass an array of categoryIds (elementIds) — even if, as in this case, there is only one category or element. You can either set the ID as a static value if it's predefined or dynamically look it up based on the title, slug or any other unique element attribute.</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
  $criteria-&gt;title = 'Hotel';
  $accomodationTypeCategory = craft()-&gt;elements-&gt;findElements($criteria);
  
  $entry = new EntryModel();
  $entry-&gt;sectionId = 3;
  $entry-&gt;typeId = 3;
  $entry-&gt;authorId = 1;
  $entry-&gt;enabled = true;
  $entry-&gt;getContent()-&gt;setAttributes(array(
      'title'             =&gt; 'Test Entry', 
      'body'              =&gt; 'Testing', 
      'address'           =&gt; '101 Silver Avenue',
      'phone'             =&gt; '0209900292092',
      'websiteAddress'    =&gt; 'www.example.com',
      'towns'             =&gt; 'Newy', 
      'accommodationType' =&gt; array(
          $accomodationTypeCategory[0]-&gt;id
      )
  );
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-04T06:49:49.643Z
Answers.LastActivityDate: 2015-05-08T00:57:50.453Z
Answers.LastEditDate: 2015-05-08T00:57:50.453Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I am stuck on saving entry with a category field.
  The error message I get is about not being able to create/update the entry in the relations table.</p>
  
  <pre><code>$accomodationTypecategory = ['title'=&gt;'Hotel']
  
  $entry = new EntryModel();
  $entry-&gt;sectionId = 3;
  $entry-&gt;typeId = 3;
  $entry-&gt;authorId = 1;
  $entry-&gt;enabled = true;
  $entry-&gt;setContent(
      $entry-&gt;getContent()-&gt;setAttributes([
          'title' =&gt; 'Test Entry', 
          'body' =&gt; "Testing", 
          'address'=&gt;"101 Silver Avenue",
          'phone'=&gt;'0209900292092',
          'websiteAddress'=&gt;'www.example.com',
          'towns'=&gt;'Newy', 
          'accommodationType'=&gt;$accomodationTypecategory
      ]);
  
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
  <p>Any help will be appreciated.</p>
  
Tags: <categories><entrymodel>
Title: Saving Entry with Category field
ThreadText: # Saving Entry with Category field
  
  (Asked by: username_0 on 2015-05-04)
  
  I am stuck on saving entry with a category field.
  The error message I get is about not being able to create/update the entry in the relations table.
  
  ```
  $accomodationTypecategory = ['title'=>'Hotel']
  
  $entry = new EntryModel();
  $entry->sectionId = 3;
  $entry->typeId = 3;
  $entry->authorId = 1;
  $entry->enabled = true;
  $entry->setContent(
      $entry->getContent()->setAttributes([
          'title' => 'Test Entry', 
          'body' => "Testing", 
          'address'=>"101 Silver Avenue",
          'phone'=>'0209900292092',
          'websiteAddress'=>'www.example.com',
          'towns'=>'Newy', 
          'accommodationType'=>$accomodationTypecategory
      ]);
  
  craft()->entries->saveEntry($entry);
  
  ```
  
  Any help will be appreciated.
  
  **Tags:** categories, entrymodel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-05-04. Score: 6 | Accepted answer
  
  To set a related category (or any other kind of element) you will want to pass an array of categoryIds (elementIds) — even if, as in this case, there is only one category or element. You can either set the ID as a static value if it's predefined or dynamically look it up based on the title, slug or any other unique element attribute.
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Category);
  $criteria->title = 'Hotel';
  $accomodationTypeCategory = craft()->elements->findElements($criteria);
  
  $entry = new EntryModel();
  $entry->sectionId = 3;
  $entry->typeId = 3;
  $entry->authorId = 1;
  $entry->enabled = true;
  $entry->getContent()->setAttributes(array(
      'title'             => 'Test Entry', 
      'body'              => 'Testing', 
      'address'           => '101 Silver Avenue',
      'phone'             => '0209900292092',
      'websiteAddress'    => 'www.example.com',
      'towns'             => 'Newy', 
      'accommodationType' => array(
          $accomodationTypeCategory[0]->id
      )
  );
  craft()->entries->saveEntry($entry);
  
  ```
  
  
  ---
  
  