{"id":1860,"date":"2024-10-03T16:47:30","date_gmt":"2024-10-03T13:47:30","guid":{"rendered":"https:\/\/plumrocket.com\/learn\/?p=1860"},"modified":"2025-04-02T18:51:25","modified_gmt":"2025-04-02T15:51:25","slug":"magento-2-custom-table-how-to-create-and-insert-data","status":"publish","type":"post","link":"https:\/\/plumrocket.com\/learn\/magento-2-insert-data-into-custom-table","title":{"rendered":"Magento 2 Custom Table: How to Create and Insert Data"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full disable_zoom\"><img loading=\"lazy\" width=\"1600\" height=\"600\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/12\/magento-2-custom-table.png\" alt=\"Magento 2 Custom Table: How to Create and Insert Data\" class=\"wp-image-2259\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/12\/magento-2-custom-table.png 1600w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/12\/magento-2-custom-table-300x113.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/12\/magento-2-custom-table-1024x384.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/12\/magento-2-custom-table-768x288.png 768w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/12\/magento-2-custom-table-1536x576.png 1536w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/12\/magento-2-custom-table-1568x588.png 1568w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/figure>\n\n\n\n<p>Magento 2 custom tables are helpful for managing the data on your website. They can extend the functionality of your store when coping with data. If you need unique product attributes or additional customer information that is beyond Magento&#8217;s capabilities, you can create a custom table to store and manage them.<\/p>\n\n\n\n<p>When developing or updating a module for Magento 2, modifying the database structure is often necessary. This may include creating or deleting tables, adding or removing fields in them, and inserting data into these custom tables.<\/p>\n\n\n\n<p>There are two approaches to inserting data into a custom table in Magento 2:<\/p>\n\n\n\n<ul><li><strong><a href=\"#creating-a-magento-2-custom-table-install-schema\">By creating the corresponding classes<\/a><\/strong> (the methods in these classes will be executed during the installation or update of the module).<\/li><li><strong><a href=\"#creating-a-magento-2-custom-table-declarative-schema\">Using declarative schema approach<\/a><\/strong>.<\/li><\/ul>\n\n\n\n<p>In this article, we will discover both methods and provide guidance on when each should be used.<\/p>\n\n\n\n<h2 id=\"creating-a-magento-2-custom-table-install-schema\">Creating a Magento 2 Custom Table (Install Schema Approach)<\/h2>\n\n\n\n<p>If you are using Magento 2.2 or another older version, the only way to create a Magento 2 custom table is to use the install schema approach. For Magento versions 2.3 or later, we recommend using a new declarative approach, described in the next section.<\/p>\n\n\n\n<p>So, let\u2019s get it in more detail.<\/p>\n\n\n\n<h3>Create a New Table with InstallSchema.php<\/h3>\n\n\n\n<p>In order to create a new table in the database, you need to create a file InstallSchema.php in the directory <code>app\/code\/CustomVendor\/CustomModule\/Setup\/<\/code>. The code below will generate a new table <code>plumrocket_custom_table<\/code> in the DB:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\nnamespace Plumrocket\\CustomModule\\Setup;\n\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\SchemaSetupInterface;\n\nclass InstallSchema implements \\Magento\\Framework\\Setup\\InstallSchemaInterface\n{\n   public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)\n   {\n       $installer = $setup;\n       $installer->startSetup();\n\n       if (!$installer->tableExists(\u2018plumrocket_custom_table\u2019)) {\n           $table = $installer->getConnection()\n               ->newTable(\n               $installer->getTable(\u2018plumrocket_custom_table\u2019)\n               )\n               ->addColumn(\n                   'id',\n                   \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_INTEGER,\n                   null,\n                   [\n                       'identity' => true,\n                       'nullable' => false,\n                       'primary'  => true,\n                       'unsigned' => true,\n                   ]\n               )\n               ->addColumn(\n                   'title',\n                   \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_TEXT,\n                   null,\n                   [\n                       'nullable' => true\n                   ]\n               )\n               ->addColumn(\n                   'test',\n                   \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_TEXT,\n                   255,\n                   [\n                       'nullable' => true\n                   ]\n               )\n               ->setComment('My Table');\n           $installer->getConnection()->createTable($table);\n       }\n\n       $installer->endSetup();\n   }\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>According to the code snippet above, if a table with such a name does not exist, it will be created with the columns id, title, and test.<\/p>\n\n\n\n<p>If your module is already installed in Magento 2, it should be removed. You can do this by deleting the corresponding entry from the <code>setup_module<\/code> table in the database.<\/p>\n\n\n\n<p>Now, the newly created table can be viewed in the list:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"298\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-1.png\" alt=\"Create a New Table with InstallSchema.php: the Newly Created is Viewed in the List\" class=\"wp-image-1861\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-1.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-1-300x74.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-1-1024x253.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-1-768x190.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<p>To update the table, you need to create the <code>app\/code\/CustomVendor\/CustomModule\/Setup\/UpgradeSchema.php<\/code> file. Here is the code snippet of the created class:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\nnamespace Plumrocket\\CustomModule\\Setup;\n\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\SchemaSetupInterface;\nuse Magento\\Framework\\Setup\\UpgradeSchemaInterface;\n\nclass UpgradeSchema implements UpgradeSchemaInterface\n{\n   public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)\n       {\n           $installer = $setup;\n           $installer->startSetup();\n\n           if(version_compare($context->getVersion(), '1.1.0', '>')) {\n               $tableName = $installer->getTable('plumrocket_custom_table');\n               if ($installer->getConnection()->isTableExists($tableName)) {\n                   $installer->getConnection()\n                       ->addColumn(\n                           $tableName,\n                           'number',\n                           [\n                               'type' => \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_INTEGER,\n                               'nullable' => true,\n                               'comment' => 'Update table'\n                           ]\n                       );\n\n                   $installer->getConnection()\n                       ->dropColumn($tableName, 'test');\n               }\n           }\n\n           $installer->endSetup();\n       }\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>This snippet will add a new column Number, and remove the column Test.<\/p>\n\n\n\n<p>Also, don\u2019t forget to change the module\u2019s version as well. You can do this by setting the <code>setup_version<\/code> parameter in the <code>app\/code\/CustomVendor\/CustomModule\/etc\/module.xml <\/code>file:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n   &lt;module name=\"CustomVendor_CustomModule\" setup_version=\"1.1.5\">\n   &lt;\/module>\n&lt;\/config>\n<\/code><\/pre><\/div>\n\n\n\n<p>Then, execute the command <code>php bin\/magento setup:upgrade<\/code>, and here\u2019s the result:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"298\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-2.png\" alt=\"Create a New Table with InstallSchema.php: the Result of Executed Command\" class=\"wp-image-1862\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-2.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-2-300x74.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-2-1024x253.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-2-768x190.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h3>Insert Data into a Custom Table in Magento 2<\/h3>\n\n\n\n<p>In order to insert data into the custom table in Magento 2, you should create the <code>app\/code\/CustomVendor\/CustomModule\/Setup\/InstallData.php<\/code> file with the following code:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\nnamespace CustomVendor\\CustomModule\\Setup;\n\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\n\nclass InstallData implements \\Magento\\Framework\\Setup\\InstallDataInterface\n{\n   public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n   {\n       $installer = $setup;\n       $installer->startSetup();\n\n       $tableName = $installer->getTable('plumrocket_custom_table');\n       if ($installer->getConnection()->isTableExists($tableName)) {\n           $data = [\n               [\n                   'title' => 'First title'\n               ],\n               [\n                   'title' => 'Second title',\n               ],\n               [\n                   'title' => 'Third title',\n               ],\n           ];\n           foreach ($data as $item) {\n               $installer->getConnection()->insert($tableName, $item);\n           }\n       }\n\n       $installer->endSetup();\n   }\n}<\/code><\/pre><\/div>\n\n\n\n<p>In this example, you can see the simple data that has been inserted into our custom table. <strong>Please note<\/strong>: if it exists, you should remove the module from the setup_module table.<\/p>\n\n\n\n<p>The next step is to execute a command <code>bin\/magento s:up<\/code> and check the content of the custom table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"408\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-3.png\" alt=\"Insert Data into a Custom Table in Magento 2: Execute a Command and Check the Content\" class=\"wp-image-1863\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-3.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-3-300x101.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-3-1024x346.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-3-768x260.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<p>To insert data into the custom table during the Magento 2 module upgrade, you should create a file similar to the one used for updating the table structure. So, create the <code>UpgradeData.php<\/code> file in the <code>app\/code\/CustomVendor\/CustomModule\/Setup<\/code> directory.<\/p>\n\n\n\n<p>Here is an example of file content:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\nnamespace CustomVendor\/CustomModule\\Setup;\n\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Framework\\Setup\\UpgradeDataInterface;\n\nclass UpgradeData implements UpgradeDataInterface\n{\n   public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n   {\n       $installer = $setup;\n       $installer->startSetup();\n\n       if (version_compare($context->getVersion(), '1.1.0', '>')) {\n           $tableName = $installer->getTable(\u2018plumrocket_custom_table\u2019);\n           if ($installer->getConnection()->isTableExists($tableName)) {\n               $data = [\n                   [\n                       'number' => 5\n                   ],\n                   [\n                       'number' => -10,\n                   ],\n                   [\n                       'number' => null,\n                   ],\n               ];\n               foreach ($data as $item) {\n                   $installer->getConnection()->insert($tableName, $item);\n               }\n           }\n       }\n\n       $installer->endSetup();\n   }\n}<\/code><\/pre><\/div>\n\n\n\n<p>In this very instance, we inserted data into the Number field of our custom table. Remember to update the module version in <code>module.xml<\/code>. Then, run <code>php bin\/magento s:up<\/code> command and keep track of the result:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"491\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-4.png\" alt=\"Insert Data into a Custom Table in Magento 2: Run the Command\" class=\"wp-image-1864\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-4.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-4-300x122.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-4-1024x417.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-4-768x313.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h2 id=\"creating-a-magento-2-custom-table-declarative-schema\">Creating a Magento 2 Custom Table (New Declarative Schema Approach)<\/h2>\n\n\n\n<p>In Magento versions 2.2 or earlier, the only way to create the custom table was via the install schema approach we discussed in the previous section. It included writing complex codes to frame out the table structure.<\/p>\n\n\n\n<p>With the release of Magento 2.3, a new declarative approach was introduced, often preferred for its flexibility and maintainability. It&#8217;s important to proceed with caution, though. Choosing the right approach and implementing it without adhering strictly to Magento best practices can introduce technical issues or performance bottlenecks. To ensure a robust solution, careful consideration is needed, and partnering with <a href=\"\/magento-services\" target=\"_blank\" rel=\"noreferrer noopener\">Magento specialists<\/a> is often recommended. <\/p>\n\n\n\n<p>Let\u2019s take a closer look at this new approach.<\/p>\n\n\n\n<h3>Create a New Table with db_schema.xml<\/h3>\n\n\n\n<p>First, create a <code>db_schema.xml<\/code> file in the <code>app\/code\/CustomVendor\/CustomModule\/etc<\/code> directory. Here is the code snippet from the created file:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;schema xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n       xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Setup\/Declaration\/Schema\/etc\/schema.xsd\">\n   &lt;table name=\"new_custom_table\" resource=\"default\" comment=\"My table using db_schema\">\n       &lt;column xsi:type=\"int\" name=\"id\" unsigned=\"false\" nullable=\"false\" identity=\"true\" comment=\"Some ID\" \/>\n       &lt;column xsi:type=\"int\" name=\"random_integer\" comment=\"Some random integer\" \/>\n       &lt;column xsi:type=\"timestamp\" name=\"created_at\" nullable=\"false\" default=\"CURRENT_TIMESTAMP\" comment=\"Created At\" \/>\n       &lt;constraint xsi:type=\"primary\" referenceId=\"PRIMARY\">\n           &lt;column name=\"id\" \/>\n       &lt;\/constraint>\n   &lt;\/table>\n&lt;\/schema><\/code><\/pre><\/div>\n\n\n\n<p>As you can see from the code, we are creating a new table &#8216;new_custom_table&#8217;, utilizing the <code>table<\/code><em> <\/em>node. Within the <code>table<\/code> node, there are <code>column<\/code><em> <\/em>subnodes that define the columns in the new table. In this case, the table will contain the <code>id<\/code><em>, <\/em><code>random_integer<\/code><em>,<\/em> and <em><code>created_at<\/code><\/em> fields.<\/p>\n\n\n\n<p>In the <code>constraint<\/code><em> <\/em>subnode, we specified the <code>id<\/code> field as the primary key. Also, the <code>type<\/code><em> <\/em>attribute of this subnode can have a <code>unique<\/code><em> <\/em>or <code>foreign<\/code><em> <\/em>value.<\/p>\n\n\n\n<p>Before updating the database structure, you need to add a new table schema to the <code>db_whitelist_schema.json<\/code> file by running the following command:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">php bin\/magento setup:db-declaration:generate-whitelist --module-name=CustomVendor_CustomModule\n<\/code><\/pre><\/div>\n\n\n\n<p>To generate a new table, run the following command in the console:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">php bin\/magento setup:upgrade<\/code><\/pre><\/div>\n\n\n\n<p>Or its shortened version:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">php bin\/magento s:up<\/code><\/pre><\/div>\n\n\n\n<p>Here is the result of the executed command:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"298\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-5.png\" alt=\"Create a New Table with db_schema.xml: the Result of the Executed Command\" class=\"wp-image-1865\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-5.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-5-300x74.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-5-1024x253.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-5-768x190.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h3>Insert Data into Custom Table in Magento 2<\/h3>\n\n\n\n<p>Using the new declarative schema approach, you can insert data into a custom table using the Patch system. This method differs in that you need to create a <code>Model<\/code>, <code>ResourceModel<\/code>, and <code>Collection<\/code>.<\/p>\n\n\n\n<p><strong>Model class:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">se Magento\\Framework\\Model\\AbstractModel;\n\nclass CustomModel extends AbstractModel\n{\n   protected function _construct()\n   {\n       $this->_init(CustomVendor\\CustomModule\\Model\\ResourceModel\\CustomModel');\n   }\n}\n<\/code><\/pre><\/div>\n\n\n\n<p><strong>ResourceModel class:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">use Magento\\Framework\\Model\\ResourceModel\\Db\\AbstractDb;\n\nclass CustomModel extends AbstractDb\n{\n   public function __construct(\\Magento\\Framework\\Model\\ResourceModel\\Db\\Context $context)\n   {\n       parent::__construct($context);\n   }\n\n   protected function _construct()\n   {\n       $this->_init(\u2018plumrocket_custom_table\u2019, 'id');\n   }\n}\n<\/code><\/pre><\/div>\n\n\n\n<p><strong>Collection class:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">use Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection;\n\nclass Collection extends AbstractCollection\n{\n   protected function _construct()\n   {\n       $this->_init(\u2018CustomVendor\\CustomModule\\Model\\CustomModel\u2019,\u2019CustomVendor\\CustomModule\\Model\\ResourceModel\\CustomModel\u2019);\n   }\n}\nTo insert data into a Magento 2 custom table, create a new AddData.php class in the CustomVendor\/CustomModule\/Setup\/Patch\/Data directory and insert the following code:\n&lt;?php\n\nnamespace CustomVendor\\CustomModule\\Setup\\Patch\\Data;\n\nuse Magento\\Framework\\Setup\\Patch\\DataPatchInterface;\nuse Magento\\Framework\\Setup\\Patch\\PatchVersionInterface;\n\nclass AddData implements DataPatchInterface, PatchVersionInterface\n{\n   private $customModel;\n\n   public function __construct(\n       \\CustomVendor\\CustomModule\\Model\\CustomModel $model\n   ) {\n       $this->customModel = $model;\n   }\n\n   public function apply()\n   {\n       $modelData = [];\n       $modelData['title'] = 'Model title';\n       $modelData['number'] = 2024;\n       $this->customModel->addData($modelData);\n\n       $this->customModel->getResource()->save($this->customModel);\n   }\n\n   public static function getDependencies()\n   {\n       return [];\n   }\n\n   public static function getVersion()\n   {\n       return '1.0.1';\n   }\n\n   public function getAliases()\n   {\n       return [];\n   }\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>After running the <code>php bin\/magento s:up<\/code> command, a new entry will appear in the custom table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"513\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-6.png\" alt=\"Insert Data into Custom Table in Magento 2: a New Entry will Appear in the Custom Table\" class=\"wp-image-1866\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-6.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-6-300x128.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-6-1024x436.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/10\/magento-2-custom-table-6-768x327.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>That\u2019s it. We should also note that both methods to insert data into a custom table are valid for Magento version 2.3 or later, so you can choose the most convenient one.<\/p>\n\n\n\n<div class=\"wp-block-cover has-background-dim\" style=\"background-color:#fff9f3;min-height:100px\"><div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-dark-gray-color has-text-color\">If you have trouble with Magento 2 custom tables or other questions, feel free to <a href=\"https:\/\/plumrocket.com\/contacts\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"has-inline-color has-secondary-color\">reach out to us<\/span><\/a>. Our development team will be happy to help!<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p><!-- wp:paragraph --><\/p>\n<p>Magento 2 custom tables are helpful for managing the data on your website. They can extend the functionality of your store when coping with data. If you need unique product attributes or additional customer information that is beyond Magento&#8217;s capabilities, you can create a custom table to store and manage them.<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[163],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Create &amp; Insert Data into Custom Table in Magento 2<\/title>\n<meta name=\"description\" content=\"Learn how to create Magento 2 custom tables and insert data with two effective approaches: using declarative schema or creating the corresponding classes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/plumrocket.com\/learn\/magento-2-insert-data-into-custom-table\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create &amp; Insert Data into Custom Table in Magento 2\" \/>\n<meta property=\"og:description\" content=\"Learn how to create Magento 2 custom tables and insert data with two effective approaches: using declarative schema or creating the corresponding classes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/plumrocket.com\/learn\/magento-2-insert-data-into-custom-table\" \/>\n<meta property=\"og:site_name\" content=\"Magento Tutorials for Beginners &amp; Professionals\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-03T13:47:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-02T15:51:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/12\/magento-2-custom-table.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1860"}],"collection":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/comments?post=1860"}],"version-history":[{"count":17,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1860\/revisions"}],"predecessor-version":[{"id":2371,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1860\/revisions\/2371"}],"wp:attachment":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/media?parent=1860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/categories?post=1860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/tags?post=1860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}