app/code/{Vendor}/{ModuleName}/Model/{YourModel}.php
app/code/{Vendor}/{ModuleName}/Model/ResourceModel/{YourResourceModelName}.php
app/code/{Vendor}/{ModuleName}/Model/ResourceModel/{YourResourceModelName}/Collection.php
file Location: app/code/{Vendor}/{ModuleName}/Model/{YourModel}.php
Format
<?php
/**
* Create model name of Student
*/
namespace {Vendor}\{ModuleName}\Model;
class {YourModel} extends \Magento\Framework\Model\AbstractModel
{
/**
* Map Resource Class At Model lass
*/
public function _construct()
{
$this->_init(\{Vendor}\{ModuleName}\Model\ResourceModel\{YourModel}::class);
}
}
Example: A model name is Student
file Location: app/code/StackExchange/Example/Model/Student.php
<?php
/**
* Create model name of Student
*/
namespace StackExchange\Example\Model;
class Student extends \Magento\Framework\Model\AbstractModel
{
/**
* Map Resource Class At Model lass
*/
public function _construct()
{
$this->_init(\StackExchange\Example\Model\ResourceModel\Student::class);
}
}
file Location: app/code/{Vendor}/{ModuleName}/Model/ResourceModel/{YourResourceModelName}.php
Format
<?php
/**
* Create Resource Model name for model Student \StackExchange\Example\Model\Student
* As Model name Student then Resource model is Student
*/
namespace {Vendor}\{ModuleName}\Model\ResourceModel;
class {YourResourceModelName} extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* Define table for this Resource model
* @return void
*/
public function _construct()
{
$this->_init('{DatabaseTableName}', '{Primary_key}');
}
}
Example: A ResourceModel Class name is Student
file Location: app/code/StackExchange/Example/Model/ResourceModel/Student.php
<?php
/**
* Create Resource Model name for model Student \StackExchange\Example\Model\Student
* As Model name Student then Resource model is Student
*/
namespace StackExchange\Example\Model\ResourceModel;
class Student extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* Define table for this Resource model
* @return void
*/
public function _construct()
{
$this->_init('example', 'id');
}
}
file Location: app/code/{Vendor}/{ModuleName}/Model/ResourceModel/{YourResourceModelName}/Collection.php
Format
<?php
namespace {Vendor}\{ModuleName}\Model\ResourceModel\{YourResourceModelName};
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* Map Model CLass and Resource mode Class at COllection Class
*/
public function _construct()
{
$this->_init(
\{Vendor}\{ModuleName}\Model\{YourModel}::class,
\{Vendor}\{ModuleName}\Model\ResourceModel\{YourResourceModelName}::class
);
}
}
Example: A Collection class for is Student
file Location: /app/code/StackExchange/Example/Model/ResourceModel/Student/Collection.php
<?php
/*
* Collection Model Class .As Model and Resource model Class Name is Student
* That create name student folder at StackExchange\Example\Model\ResourceModel
*/
namespace StackExchange\Example\Model\ResourceModel\Student;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* Map Model CLass and Resource mode Class at COllection Class
*/
public function _construct()
{
$this->_init(
\StackExchange\Example\Model\Student::class,
\StackExchange\Example\Model\ResourceModel\Student::class
);
}
}