<p>I can answer my own question with some information I've found to help anyone else for Craft 2 related porting.</p>
<p>The <code>populateModel()</code> and <code>populateModels()</code> methods used to exist on the BaseModel class in Craft 2:</p>
<p><a href="https://docs.craftcms.com/api/v2/craft-basemodel.html#public-methods" rel="nofollow noreferrer">https://docs.craftcms.com/api/v2/craft-basemodel.html#public-methods</a></p>
<p>They were however removed in Craft 3 as discovered and don't have a replacement, this was confirmed from this GitHub issue and conversation:</p>
<p><a href="https://github.com/craftcms/cms/issues/2239" rel="nofollow noreferrer">https://github.com/craftcms/cms/issues/2239</a></p>
<p>So in Craft 3, without these methods, you'd essentially have to write a loop each time, which when dealing with multiple models could start to feel like duplicating code. However, reading the conversation around this subject, Ben Croker (putsyourlightson) ended up implementing their own BaseModel class which any required models extend from, so these methods can be used across any model that's being extended from the BaseModel class, which would reimplement the functionality.</p>
<p>Using one of the plugins from putsyourlightson as an example. A BaseModel class is created and then all other models extend from this BaseModel:</p>
<p><a href="https://github.com/putyourlightson/craft-campaign/blob/develop/src/base/BaseModel.php" rel="nofollow noreferrer">https://github.com/putyourlightson/craft-campaign/blob/develop/src/base/BaseModel.php</a></p>
<p>The remaining <code>model()</code> method, is actually from an ActiveRecord in Craft 2 and this doesn't exist either in Craft 3, from a Yii2 perspective, <code>::instance()</code> should provide the same behaviour, providing a static class instance, in Craft 2 it provided a static model of the record.</p>

CC BY-SA 4.0
2022-04-18T19:28:51.200Z
2022-04-29T09:31:54.760Z
2022-04-29T09:31:54.760Z
James White
<p>I'm attempting to port a custom plugin that was originally written for Craft 2. It has a fair bit of logic, controllers, services, records, models etc. The records and models which I admittedly don't have much experience with are the main area which I'm not fully understanding, but taking the challenge as a learning experience!</p>
<p>After porting the services classes to Craft 3 with namespaces and registering them in the plugin, I've hit the first major block.</p>
<p>The existing Craft 2 service logic seems to call methods that work in Craft 2 but don't seem to exist in Craft 3. Here's the example in Craft 2:</p>
<ul>
<li>MyPlugin_DataRecord::model()</li>
<li>MyPlugin_DataModel::populateModel()</li>
<li>MyPlugin_DataModel::populateModels()</li>
</ul>
<p>Example in a service class, which is then used in other places.</p>
<pre><code>protected function getModel()
{
    return MyPlugin_DataRecord::model();
}

protected function newModel()
{
    return new MyPlugin_DataModel();
}

protected function populateModel($record)
{
    return MyPlugin_DataModel::populateModel($record);
}

protected function populateModels($records)
{
    return MyPlugin_DataModel::populateModels($records, 'id');
}
</code></pre>
<p>These appear to be Craft 2 methods but do not seem to exist at all in Craft 3. Any pointers on how this should be replicated on Craft 3, given it doesn't look like there is any equivalent method in Craft 3 and it could be more of a case of refactor here.</p>

<craft3><craft2><models><records>
::model(), ::populateModel() and ::populateModels() in Craft 3
# ::model(), ::populateModel() and ::populateModels() in Craft 3

(Asked by: username_0 on 2022-04-11)

I'm attempting to port a custom plugin that was originally written for Craft 2. It has a fair bit of logic, controllers, services, records, models etc. The records and models which I admittedly don't have much experience with are the main area which I'm not fully understanding, but taking the challenge as a learning experience!
After porting the services classes to Craft 3 with namespaces and registering them in the plugin, I've hit the first major block.
The existing Craft 2 service logic seems to call methods that work in Craft 2 but don't seem to exist in Craft 3. Here's the example in Craft 2:

MyPlugin_DataRecord::model()
MyPlugin_DataModel::populateModel()
MyPlugin_DataModel::populateModels()

Example in a service class, which is then used in other places.
protected function getModel()
{
    return MyPlugin_DataRecord::model();
}

protected function newModel()
{
    return new MyPlugin_DataModel();
}

protected function populateModel($record)
{
    return MyPlugin_DataModel::populateModel($record);
}

protected function populateModels($records)
{
    return MyPlugin_DataModel::populateModels($records, 'id');
}

These appear to be Craft 2 methods but do not seem to exist at all in Craft 3. Any pointers on how this should be replicated on Craft 3, given it doesn't look like there is any equivalent method in Craft 3 and it could be more of a case of refactor here.

**Tags:** craft3, craft2, models, records


---

## 1 answers

### Answer by username_0 on 2022-04-18. Score: 2

I can answer my own question with some information I've found to help anyone else for Craft 2 related porting.

The `populateModel()` and `populateModels()` methods used to exist on the BaseModel class in Craft 2:

<https://docs.craftcms.com/api/v2/craft-basemodel.html#public-methods>

They were however removed in Craft 3 as discovered and don't have a replacement, this was confirmed from this GitHub issue and conversation:

<https://github.com/craftcms/cms/issues/2239>

So in Craft 3, without these methods, you'd essentially have to write a loop each time, which when dealing with multiple models could start to feel like duplicating code. However, reading the conversation around this subject, Ben Croker (putsyourlightson) ended up implementing their own BaseModel class which any required models extend from, so these methods can be used across any model that's being extended from the BaseModel class, which would reimplement the functionality.

Using one of the plugins from putsyourlightson as an example. A BaseModel class is created and then all other models extend from this BaseModel:

<https://github.com/putyourlightson/craft-campaign/blob/develop/src/base/BaseModel.php>

The remaining `model()` method, is actually from an ActiveRecord in Craft 2 and this doesn't exist either in Craft 3, from a Yii2 perspective, `::instance()` should provide the same behaviour, providing a static class instance, in Craft 2 it provided a static model of the record.


---

