Answers.Body: <p>Would like to know if this is the best solution? Upvote if you think it is.</p>
  
  <p>The solution is make your own custom populateModel method to catch the eager loaded attributes</p>
  
  <pre><code>/**
   * @param array|Market_VariantRecord $values
   *
   * @return $this
   */
  public static function populateModel($values)
  {
      $model = parent::populateModel($values);
      if (is_object($values) &amp;&amp; $values instanceof Market_ProductRecord) {
          $model-&gt;product = $values-&gt;product;
      }
  
      return $model;
  }
  </code></pre>
  
  <p>I suppose the Craft core BaseModel could check if the value of type BaseRecord and do the same?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-10T16:42:37.730Z
Answers.Text: I think you're spot on both with the original workaround as well as the solution in your answer. Craft currently doesn't have support for this in populateModel(s), although we'd have to make a more generic solution that would work for any record (not just Market_ProductRecord) in your answer.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-11T00:09:08.333Z
Answers.Text: thats for the comment brad, I am moving to the answer solution to all my models that have eager loaded relations.
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-10T08:48:32.977Z
Answers.LastActivityDate: 2015-02-10T08:48:32.977Z
Answers.UserDisplayName: Luke Holder
Body: <p>When retrieving a record in a service and returning a model the static methods: populateModel and populateModels on BaseModel are useful.</p>
  
  <p>When eager loading a related model in the record query, for example:</p>
  
  <p><code>$variantRecord = Market_VariantRecord::model()-&gt;with('product')-&gt;findById($id);</code></p>
  
  <p>This will load the related Record's product into the <code>$variantRecord</code> accessible with <code>$variantRecord-&gt;product</code></p>
  
  <p>when casting this variant record into a Model, using <code>VariantModel::populateModel</code>, will the product also being retrieved be cast into the <code>model-&gt;product (AttributeType::Mixed)</code> of the variant?</p>
  
  <p>Or should we manually be manually assigning the ->product attribute as casting it to a model with populate model like this?</p>
  
  <pre><code>$variant = Market_VariantRecord::model()-&gt;with('product')-&gt;findById($id);
  $variantModel = Market_VariantModel::populateModel($variant);
  $variantModel-&gt;product = Market_ProductModel::populateModel($variant-&gt;product);
  return $variantModel;
  </code></pre>
  
  <p>This seems clunky and am looking for suggestions on improving the population of models from records with eager loading of relations.</p>
  
  <p>This is one area </p>
  
Tags: <plugin-development>
Title: populateModel usage with eager loading related records
ThreadText: # populateModel usage with eager loading related records
  
  (Asked by: username_0 on 2015-02-10)
  
  When retrieving a record in a service and returning a model the static methods: populateModel and populateModels on BaseModel are useful.
  
  When eager loading a related model in the record query, for example:
  
  `$variantRecord = Market_VariantRecord::model()->with('product')->findById($id);`
  
  This will load the related Record's product into the `$variantRecord` accessible with `$variantRecord->product`
  
  when casting this variant record into a Model, using `VariantModel::populateModel`, will the product also being retrieved be cast into the `model->product (AttributeType::Mixed)` of the variant?
  
  Or should we manually be manually assigning the ->product attribute as casting it to a model with populate model like this?
  
  ```
  $variant = Market_VariantRecord::model()->with('product')->findById($id);
  $variantModel = Market_VariantModel::populateModel($variant);
  $variantModel->product = Market_ProductModel::populateModel($variant->product);
  return $variantModel;
  
  ```
  
  This seems clunky and am looking for suggestions on improving the population of models from records with eager loading of relations.
  
  This is one area
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-02-10. Score: 2 | Accepted answer
  
  Would like to know if this is the best solution? Upvote if you think it is.
  
  The solution is make your own custom populateModel method to catch the eager loaded attributes
  
  ```
  /**
   * @param array|Market_VariantRecord $values
   *
   * @return $this
   */
  public static function populateModel($values)
  {
      $model = parent::populateModel($values);
      if (is_object($values) && $values instanceof Market_ProductRecord) {
          $model->product = $values->product;
      }
  
      return $model;
  }
  
  ```
  
  I suppose the Craft core BaseModel could check if the value of type BaseRecord and do the same?
  
  > **Comments:**
  > **username_1**: I think you're spot on both with the original workaround as well as the solution in your answer. Craft currently doesn't have support for this in populateModel(s), although we'd have to make a more generic solution that would work for any record (not just Market_ProductRecord) in your answer.  
  > **username_0**: thats for the comment username_1, I am moving to the answer solution to all my models that have eager loaded relations.  
  
  ---
  
  