Unlike EAV models, creating simple models is pretty straightforward. Let's go ahead and create a model, resource model, and a collection for a Log entity.
We will start off by creating the <MAGELICIOUS_DIR>/Core/Model/Log.php file with the following content:
class Log extends \Magento\Framework\Model\AbstractModel {
protected $_eventPrefix = 'magelicious_core_log';
protected $_eventObject = 'log';
protected function _construct() {
$this->_init(\Magelicious\Core\Model\ResourceModel\Log::class);
}
}
The use of $_eventPrefix and $_eventObject is not mandatory, but it is highly recommended. These values are used by the Magento\Framework\Model\AbstractModel event dispatcher and add to the future extensibility of our module. While Magento uses the <ModuleName>_<ModelName> convention for $_eventPrefix naming, we might be safer using <VendorName>_<ModuleName>_<ModelName>. The $_eventObject, by convention, usually bears the name of the model itself.
We then create the <MAGELICIOUS_DIR>/Core/Model/ResourceModel/Log.php file with the following content:
class Log extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb {
protected function _construct() {
$this->_init('magelicious_core_log', 'entity_id');
}
}
The _init method here takes two arguments: the magelicious_core_log value for the $mainTable argument and the entity_id value for the $idFieldName argument. The $idFieldName is the name of the primary column in the designated database. It's worth noting that the magelicious_core_log table still doesn't exist, but we will address that in a bit.
We will then create the <MAGELICIOUS_DIR>/Core/Model/ResourceModel/Log/Collection.php file with the following content:
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection {
protected function _construct() {
$this->_init(
\Magelicious\Core\Model\Log::class,
\Magelicious\Core\Model\ResourceModel\Log::class
);
}
}
The _init method here takes two arguments: the string names of $model and $resourceModel. Magento uses the <FULLY_QUALIFIED_CLASS_NAME>::class syntax for this, as it uses a nifty solution instead of passing class strings around.