{"id":282306,"date":"2021-02-06T12:10:36","date_gmt":"2021-02-06T12:10:36","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=282306"},"modified":"2026-02-10T09:58:20","modified_gmt":"2026-02-10T09:58:20","slug":"magento-2-model-resource-model-collection","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/","title":{"rendered":"Magento 2 Model, Resource Model, and Collection"},"content":{"rendered":"\n<p><strong>How to perform CRUD operations in <a href=\"https:\/\/business.adobe.com\/in\/products\/magento\/magento-commerce.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2<\/a><\/strong> &#8211; Now that we have created the tables. We need some way to manipulate (i.e. to perform CRUD operations) the table data of the database.<\/p>\n\n\n\n<p>To perform the CRUD (Create, Read, Update, Delete) operations we have to create these three classes (i.e. Model, Resource Model, and Collection) for each table. <\/p>\n\n\n\n<p>The name, Model comes from MVC (Model-View-Controller) architecture.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Model<\/h2>\n\n\n\n<p>The model represents a single entity. By that, I mean <strong>a single row of the table<\/strong>. The model provides a layer of abstraction over the resource model. <\/p>\n\n\n\n<p>Let&#8217;s create a model for the &#8220;blogmanager_blog&#8221; table. For that inside our module folder create a folder named Model. Inside that folder create a <strong>Blog.php<\/strong> file,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"231\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-1200x231.png\" alt=\"Selection_092\" class=\"wp-image-390526\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-1200x231.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-300x58.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-250x48.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-768x148.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092.png 1516w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p>Code for <em>Model\/Blog.php<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Webkul\\BlogManager\\Model;\n\nclass Blog extends \\Magento\\Framework\\Model\\AbstractModel implements \\Magento\\Framework\\DataObject\\IdentityInterface\n{\n    \/**\n     * No route page id.\n     *\/\n    const NOROUTE_ENTITY_ID = &#039;no-route&#039;;\n    \n    \/**\n     * Entity Id\n     *\/\n    const ENTITY_ID = &#039;entity_id&#039;;\n    \n    \/**\n     * BlogManager Blog cache tag.\n     *\/\n    const CACHE_TAG = &#039;webkul_blogmanager_blog&#039;;\n    \n    \/**\n     * @var string\n     *\/\n    protected $_cacheTag = &#039;webkul_blogmanager_blog&#039;;\n\n    \/**\n     * @var string\n     *\/\n    protected $_eventPrefix = &#039;webkul_blogmanager_blog&#039;;\n    \n    \/**\n     * Dependency Initilization\n     *\n     * @return void\n     *\/\n    public function _construct()\n    {\n        $this-&gt;_init(\\Webkul\\BlogManager\\Model\\ResourceModel\\Blog::class);\n    }\n    \n    \/**\n     * Load object data.\n     *\n     * @param int $id\n     * @param string|null $field\n     * @return $this\n     *\/\n    public function load($id, $field = null)\n    {\n        if ($id === null) {\n            return $this-&gt;noRoute();\n        }\n        return parent::load($id, $field);\n    }\n    \n    \/**\n     * No Route\n     *\n     * @return $this\n     *\/\n    public function noRoute()\n    {\n        return $this-&gt;load(self::NOROUTE_ENTITY_ID, $this-&gt;getIdFieldName());\n    }\n\n    \/**\n     * Get Identities\n     *\n     * @return array\n     *\/\n    public function getIdentities()\n    {\n        return &#091;self::CACHE_TAG.&#039;_&#039;.$this-&gt;getId()];\n    }\n\n    \/**\n     * Get Id\n     *\n     * @return int|null\n     *\/\n    public function getId()\n    {\n        return parent::getData(self::ENTITY_ID);\n    }\n\n    \/**\n     * Set Id\n     *\n     * @param int $id\n     * @return \\Webkul\\BlogManager\\Model\\Blog\n     *\/\n    public function setId($id)\n    {\n        return $this-&gt;setData(self::ENTITY_ID, $id);\n    }\n}<\/pre>\n\n\n\n<p>The model class will extend\u00a0<em>\\<code>M<\/code><\/em><code><em>agento\\Framework\\Model\\AbstractModel<\/em><\/code>\u00a0and implements\u00a0<code><em>\\Magento\\Framework\\DataObject\\IdentityInterface<\/em><\/code>.<\/p>\n\n\n\n<p> The IdentityInterface will force the Model class to define the\u00a0<code><em>getIdentities()<\/em><\/code>\u00a0method which will return a unique id for each model.<\/p>\n\n\n\n<p>Earlier I said &#8220;The model provides a layer of abstraction over the resource model.&#8221; <\/p>\n\n\n\n<p>By that I meant that we use the model class in our code to manipulate the tables but the actual query gets executed by the resource model class.<\/p>\n\n\n\n<p>So every model will require a resource model which will execute the actual SQL queries. <\/p>\n\n\n\n<p>When we create a new object the _construct method will get executed, which will call the <em>init<\/em> method where we have to pass the resource model as param.<\/p>\n\n\n\n<p>Similarly, we need to create the model for the &#8220;blogmanager_comment&#8221; table as Model\/Comment.php,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"243\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_093-1200x243.png\" alt=\"Selection_093\" class=\"wp-image-390529\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_093-1200x243.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_093-300x61.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_093-250x51.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_093-768x155.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_093.png 1517w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">File Structure<\/figcaption><\/figure>\n\n\n\n<p>Code for <em>Model\/Comment.php<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Webkul\\BlogManager\\Model;\n\nclass Comment extends \\Magento\\Framework\\Model\\AbstractModel implements \\Magento\\Framework\\DataObject\\IdentityInterface\n{\n    \/**\n     * No route page id.\n     *\/\n    const NOROUTE_ENTITY_ID = &#039;no-route&#039;;\n    \n    \/**\n     * Entity Id\n     *\/\n    const ENTITY_ID = &#039;entity_id&#039;;\n    \n    \/**\n     * BlogManager Comment cache tag.\n     *\/\n    const CACHE_TAG = &#039;webkul_blogmanager_comment&#039;;\n    \n    \/**\n     * @var string\n     *\/\n    protected $_cacheTag = &#039;webkul_blogmanager_comment&#039;;\n\n    \/**\n     * @var string\n     *\/\n    protected $_eventPrefix = &#039;webkul_blogmanager_comment&#039;;\n    \n    \/**\n     * Dependency Initilization\n     *\n     * @return void\n     *\/\n    public function _construct()\n    {\n        $this-&gt;_init(\\Webkul\\BlogManager\\Model\\ResourceModel\\Comment::class);\n    }\n    \n    \/**\n     * Load object data.\n     *\n     * @param int $id\n     * @param string|null $field\n     * @return $this\n     *\/\n    public function load($id, $field = null)\n    {\n        if ($id === null) {\n            return $this-&gt;noRoute();\n        }\n        return parent::load($id, $field);\n    }\n    \n    \/**\n     * No Route\n     *\n     * @return $this\n     *\/\n    public function noRoute()\n    {\n        return $this-&gt;load(self::NOROUTE_ENTITY_ID, $this-&gt;getIdFieldName());\n    }\n\n    \/**\n     * Get Identities\n     *\n     * @return array\n     *\/\n    public function getIdentities()\n    {\n        return &#091;self::CACHE_TAG.&#039;_&#039;.$this-&gt;getId()];\n    }\n\n    \/**\n     * Get Id\n     *\n     * @return int|null\n     *\/\n    public function getId()\n    {\n        return parent::getData(self::ENTITY_ID);\n    }\n\n    \/**\n     * Set Id\n     *\n     * @param int $id\n     * @return \\Webkul\\BlogManager\\Model\\Blog\n     *\/\n    public function setId($id)\n    {\n        return $this-&gt;setData(self::ENTITY_ID, $id);\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Resource Model<\/h2>\n\n\n\n<p>We have already discussed the importance of the resource model. So let&#8217;s just dive into code.<\/p>\n\n\n\n<p>First, we will create the resource model for the &#8220;blogmanager_blog&#8221; table.<\/p>\n\n\n\n<p>For that, we have to create a ResourceModel folder under the Model folder, and inside that ResourceModel folder, we will create a <strong>Blog.php<\/strong> file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"274\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_094-1-1200x274.png\" alt=\"Selection_094-1\" class=\"wp-image-390531\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_094-1-1200x274.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_094-1-300x68.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_094-1-250x57.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_094-1-768x175.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_094-1.png 1518w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p>Code for <em>Model\/ResourceModel\/Blog.php<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Webkul\\BlogManager\\Model\\ResourceModel;\n\nclass Blog extends \\Magento\\Framework\\Model\\ResourceModel\\Db\\AbstractDb\n{\n    \/**\n     * Dependency Initilization\n     *\n     * @return void\n     *\/\n    public function _construct()\n    {\n        $this-&gt;_init(&quot;blogmanager_blog&quot;, &quot;entity_id&quot;);\n    }\n}<\/pre>\n\n\n\n<p>Here we have to mention our table name and the primary key of the table.<\/p>\n\n\n\n<p>All the resource model classes will extend<em>\\Magento\\Framework\\Model\\ResourceModel\\Db\\AbstractDb <\/em>class which have the common functions for actual database operations.<\/p>\n\n\n\n<p>Similarly, we need to create the  resource model for the &#8220;blogmanager_comment&#8221; table as <strong>Model\/ResourceModel<\/strong>\/<strong>Comment.php<\/strong>,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"285\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_095-1200x285.png\" alt=\"Selection_095\" class=\"wp-image-390533\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_095-1200x285.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_095-300x71.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_095-250x59.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_095-768x182.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_095.png 1517w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p>Code for <em>Model\/ResourceModel\/Comment.php<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Webkul\\BlogManager\\Model\\ResourceModel;\n\nclass Comment extends \\Magento\\Framework\\Model\\ResourceModel\\Db\\AbstractDb\n{\n    \/**\n     * Dependency Initilization\n     *\n     * @return void\n     *\/\n    public function _construct()\n    {\n        $this-&gt;_init(&quot;blogmanager_comment&quot;, &quot;entity_id&quot;);\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Collection<\/h2>\n\n\n\n<p>We know that the model represents a single row of the table whereas the collection represents all the rows of the table. <\/p>\n\n\n\n<p>We can think of the collection as a collection of models or &#8220;Select * From Table table_name&#8221;.<\/p>\n\n\n\n<p>So if we have to access all\/multiple rows of the table, perform some filter with where\/like, or perform some sorting with the order by then we should use collection. <\/p>\n\n\n\n<p>Whereas if we have to insert a single row or fetch a single row or update some column in a row or delete a single row then we have to use a model.<\/p>\n\n\n\n<p>Now let&#8217;s create a collection class for the &#8220;blogmanager_blog&#8221; table. For that, we have to create a folder with the same name as the resource model class name under the ResourceModel folder. <\/p>\n\n\n\n<p>So here we will create a folder named Blog under Model\/ResourceModel\/ folder. And under that folder, we will create <strong>Collection.php<\/strong> class.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"321\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_096-1200x321.png\" alt=\"Selection_096\" class=\"wp-image-390537\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_096-1200x321.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_096-300x80.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_096-250x67.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_096-768x205.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_096.png 1519w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p>Code for <em>Model\/ResourceModel\/Blog\/Collection.php<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Webkul\\BlogManager\\Model\\ResourceModel\\Blog;\n\nclass Collection extends \\Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection\n{\n    \/**\n     * @var string\n     *\/\n    protected $_idFieldName = &#039;entity_id&#039;;\n    \n    \/**\n     * Dependency Initilization\n     *\n     * @return void\n     *\/\n    public function _construct()\n    {\n        $this-&gt;_init(\n            \\Webkul\\BlogManager\\Model\\Blog::class,\n            \\Webkul\\BlogManager\\Model\\ResourceModel\\Blog::class\n        );\n        $this-&gt;_map&#091;&#039;fields&#039;]&#091;&#039;entity_id&#039;] = &#039;main_table.entity_id&#039;;\n    }\n}<\/pre>\n\n\n\n<p>Here we have to provide both the model and the resource model when we call the <em>init<\/em> method in the constructor.<\/p>\n\n\n\n<p>Similarly, we can create the collection for the &#8220;blogmanager_comment&#8221; table as <strong>Model\/ResourceModel<\/strong>\/<strong>Comment<\/strong>\/<strong>Collection.php<\/strong>,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"348\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_097-1200x348.png\" alt=\"Selection_097\" class=\"wp-image-390539\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_097-1200x348.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_097-300x87.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_097-250x72.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_097-768x222.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_097.png 1519w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p>Code for <em>Model\/ResourceModel\/Comment\/Collection.php<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Webkul\\BlogManager\\Model\\ResourceModel\\Comment;\n\nclass Collection extends \\Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection\n{\n    \/**\n     * @var string\n     *\/\n    protected $_idFieldName = &#039;entity_id&#039;;\n    \n    \/**\n     * Dependency Initilization\n     *\n     * @return void\n     *\/\n    public function _construct()\n    {\n        $this-&gt;_init(\n            \\Webkul\\BlogManager\\Model\\Comment::class,\n            \\Webkul\\BlogManager\\Model\\ResourceModel\\Comment::class\n        );\n        $this-&gt;_map&#091;&#039;fields&#039;]&#091;&#039;entity_id&#039;] = &#039;main_table.entity_id&#039;;\n    }\n}<\/pre>\n\n\n\n<p>After creating all of the classes we need to run the di compile command. Which if you remembered was <strong>php bin\/magento setup:di:compile<\/strong><\/p>\n\n\n\n<p>The di compile will generate some codes automatically which you can find in <em>generated\/code<\/em> folder under the Magento root directory. <\/p>\n\n\n\n<p>We will learn in more detail about code generation in the later part of this blog series.<\/p>\n\n\n\n<p><strong>PS. Please check the namespace of the class if you get confused with the folder structure.<\/strong><\/p>\n\n\n\n<p>Here we learned why and how to create these classes. In the next blogs, we will see how to use them.<\/p>\n\n\n\n<p>The folder structure till now should be like this, <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"342\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_098-1200x342.png\" alt=\"Selection_098\" class=\"wp-image-390540\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_098-1200x342.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_098-300x85.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_098-250x71.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_098-768x219.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_098.png 1521w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next Blog -&gt; <a href=\"https:\/\/webkul.com\/blog\/magento-development-05-layout-and-templates\/\">Magento 2 Development 08: Layout and Templates<\/a><\/p>\n\n\n\n<p>Previous Blog -&gt; <a href=\"https:\/\/webkul.com\/blog\/magento-development-03-creating-tables\/\">Magento 2 Development 06: Creating Tables<\/a><\/p>\n\n\n\n<p>You can get more magento2 (Adobe Commerce) articles&nbsp;<a href=\"https:\/\/webkul.com\/blog\/?s=magento2\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n<div class=\"taxonomy-post_tag wp-block-post-terms\"><a href=\"https:\/\/webkul.com\/blog\/tag\/collection\/\" rel=\"tag\">collection<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/webkul.com\/blog\/tag\/model\/\" rel=\"tag\">model<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/webkul.com\/blog\/tag\/outlook-login\/\" rel=\"tag\">outlook login<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/webkul.com\/blog\/tag\/resource-model\/\" rel=\"tag\">Resource Model<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/webkul.com\/blog\/tag\/wordpress-social-login\/\" rel=\"tag\">wordpress social login<\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>How to perform CRUD operations in Magento 2 &#8211; Now that we have created the tables. We need some way to manipulate (i.e. to perform CRUD operations) the table data of the database. To perform the CRUD (Create, Read, Update, Delete) operations we have to create these three classes (i.e. Model, Resource Model, and Collection) <a href=\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":201,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[3495,5121,15563,14482,15562],"class_list":["post-282306","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-collection","tag-model","tag-outlook-login","tag-resource-model","tag-wordpress-social-login"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Magento 2 Model, Resource Model, and Collection - Webkul Blog<\/title>\n<meta name=\"description\" content=\"Learn how Magento 2 Model, Resource Model, and Collection work together to handle database operations efficiently with practical examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Magento 2 Model, Resource Model, and Collection - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Learn how Magento 2 Model, Resource Model, and Collection work together to handle database operations efficiently with practical examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-06T12:10:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-10T09:58:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-1200x231.png\" \/>\n<meta name=\"author\" content=\"Sanjay Chouhan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sanjay Chouhan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/\"},\"author\":{\"name\":\"Sanjay Chouhan\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/645580979f637b0e355deea21bd07462\"},\"headline\":\"Magento 2 Model, Resource Model, and Collection\",\"datePublished\":\"2021-02-06T12:10:36+00:00\",\"dateModified\":\"2026-02-10T09:58:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/\"},\"wordCount\":758,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-1200x231.png\",\"keywords\":[\"collection\",\"model\",\"outlook login\",\"Resource Model\",\"wordpress social login\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/\",\"url\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/\",\"name\":\"Magento 2 Model, Resource Model, and Collection - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-1200x231.png\",\"datePublished\":\"2021-02-06T12:10:36+00:00\",\"dateModified\":\"2026-02-10T09:58:20+00:00\",\"description\":\"Learn how Magento 2 Model, Resource Model, and Collection work together to handle database operations efficiently with practical examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092.png\",\"width\":1516,\"height\":292,\"caption\":\"Selection_092\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Magento 2 Model, Resource Model, and Collection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/645580979f637b0e355deea21bd07462\",\"name\":\"Sanjay Chouhan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cd6ee19f99bd1fcafef819135529c952d7c875d06fedd9fd4c4eb0996bafc1bd?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cd6ee19f99bd1fcafef819135529c952d7c875d06fedd9fd4c4eb0996bafc1bd?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Sanjay Chouhan\"},\"sameAs\":[\"https:\/\/www.instagram.com\/sanjaychouhansc\/\",\"https:\/\/in.linkedin.com\/in\/scchouhansanjay\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/sanjay-chouhan180\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Magento 2 Model, Resource Model, and Collection - Webkul Blog","description":"Learn how Magento 2 Model, Resource Model, and Collection work together to handle database operations efficiently with practical examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/","og_locale":"en_US","og_type":"article","og_title":"Magento 2 Model, Resource Model, and Collection - Webkul Blog","og_description":"Learn how Magento 2 Model, Resource Model, and Collection work together to handle database operations efficiently with practical examples.","og_url":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-02-06T12:10:36+00:00","article_modified_time":"2026-02-10T09:58:20+00:00","og_image":[{"url":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-1200x231.png","type":"","width":"","height":""}],"author":"Sanjay Chouhan","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Sanjay Chouhan","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/"},"author":{"name":"Sanjay Chouhan","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/645580979f637b0e355deea21bd07462"},"headline":"Magento 2 Model, Resource Model, and Collection","datePublished":"2021-02-06T12:10:36+00:00","dateModified":"2026-02-10T09:58:20+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/"},"wordCount":758,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-1200x231.png","keywords":["collection","model","outlook login","Resource Model","wordpress social login"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/","url":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/","name":"Magento 2 Model, Resource Model, and Collection - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092-1200x231.png","datePublished":"2021-02-06T12:10:36+00:00","dateModified":"2026-02-10T09:58:20+00:00","description":"Learn how Magento 2 Model, Resource Model, and Collection work together to handle database operations efficiently with practical examples.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_092.png","width":1516,"height":292,"caption":"Selection_092"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/magento-2-model-resource-model-collection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Magento 2 Model, Resource Model, and Collection"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/645580979f637b0e355deea21bd07462","name":"Sanjay Chouhan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cd6ee19f99bd1fcafef819135529c952d7c875d06fedd9fd4c4eb0996bafc1bd?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cd6ee19f99bd1fcafef819135529c952d7c875d06fedd9fd4c4eb0996bafc1bd?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Sanjay Chouhan"},"sameAs":["https:\/\/www.instagram.com\/sanjaychouhansc\/","https:\/\/in.linkedin.com\/in\/scchouhansanjay"],"url":"https:\/\/webkul.com\/blog\/author\/sanjay-chouhan180\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/282306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/201"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=282306"}],"version-history":[{"count":19,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/282306\/revisions"}],"predecessor-version":[{"id":525591,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/282306\/revisions\/525591"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=282306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=282306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=282306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}