Answers.Body: <p>I think that's actually the way to do it, assign the id to the owning record:</p>
  
  <pre><code>$record = new Cocktails_CocktailRecord;
  $record-&gt;authorId = 10;
  $record-&gt;title = "Gin Tonic";
  $record-&gt;save();
  </code></pre>
  
  <p>When getting the record from the db, the record is turned into a model, at this point, records and models are only dealing with authorId, not the whole record. Your model will then have to define a getAuthor() method that will get the author user for you:</p>
  
  <pre><code>public function getAuthor()
  {
      if ($this-&gt;authorId) {
          return craft()-&gt;users-&gt;getUserById($this-&gt;authorId);
      }
  }
  </code></pre>
  
  <p>This way your model only loads the relationship when you actually use it:</p>
  
  <pre><code>$model-&gt;getAuthor();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-06T13:49:50.523Z
Answers.Text: Yeah setting the id directly works. I thought it didn't when I first tried it but it does.
Answers.UserDisplayName: Leevi Graham
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-06T13:19:59.513Z
Answers.LastActivityDate: 2014-11-06T13:19:59.513Z
Answers.UserDisplayName: Benjamin David
Body: <p>Is it possible to just assign the ID to the owning side of a relationship rather than querying the DB, loading the record and assigning it?</p>
  
  <p>Something similar to Doctrines <code>_getReference()</code></p>
  
  <ul>
  <li><a href="http://www.doctrine-project.org/api/orm/2.1/class-Doctrine.ORM.EntityManager.html#_getReference" rel="nofollow noreferrer">http://www.doctrine-project.org/api/orm/2.1/class-Doctrine.ORM.EntityManager.html#_getReference</a></li>
  <li><a href="https://stackoverflow.com/a/9308576/1982031">https://stackoverflow.com/a/9308576/1982031</a></li>
  </ul>
  
Tags: <plugin-development>
Title: Create Model relationship with just an ID rather than full Model?
ThreadText: # Create Model relationship with just an ID rather than full Model?
  
  (Asked by: username_0 on 2014-11-06)
  
  Is it possible to just assign the ID to the owning side of a relationship rather than querying the DB, loading the record and assigning it?
  
  Something similar to Doctrines `_getReference()`
  
  * <http://www.doctrine-project.org/api/orm/2.1/class-Doctrine.ORM.EntityManager.html#_getReference>
  * <https://stackoverflow.com/a/9308576/1982031>
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-11-06. Score: 2 | Accepted answer
  
  I think that's actually the way to do it, assign the id to the owning record:
  
  ```
  $record = new Cocktails_CocktailRecord;
  $record->authorId = 10;
  $record->title = "Gin Tonic";
  $record->save();
  
  ```
  
  When getting the record from the db, the record is turned into a model, at this point, records and models are only dealing with authorId, not the whole record. Your model will then have to define a getAuthor() method that will get the author user for you:
  
  ```
  public function getAuthor()
  {
      if ($this->authorId) {
          return craft()->users->getUserById($this->authorId);
      }
  }
  
  ```
  
  This way your model only loads the relationship when you actually use it:
  
  ```
  $model->getAuthor();
  
  ```
  
  > **Comments:**
  > **username_0**: Yeah setting the id directly works. I thought it didn't when I first tried it but it does.  
  
  ---
  
  