populateModel usage with eager loading related records 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()-&gt;with('product')-&gt;findById($id);

This will load the related Record's product into the $variantRecord accessible with $variantRecord-&gt;product

when casting this variant record into a Model, using VariantModel::populateModel, will the product also being retrieved be cast into the model-&gt;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()-&gt;with('product')-&gt;findById($id);
$variantModel = Market_VariantModel::populateModel($variant);
$variantModel-&gt;product = Market_ProductModel::populateModel($variant-&gt;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
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) &amp;&amp; $values instanceof Market_ProductRecord) {
        $model-&gt;product = $values-&gt;product;
    }

    return $model;
}


I suppose the Craft core BaseModel could check if the value of type BaseRecord and do the same?