How to insert data into the custom table using Model, Resource Model in Magento 2?

Or how to use Model, Resource Model, Collection for the custom module in Magento 2?

Magento 2 Insert Data Model, Resource Model, Collection

Magento 2 Insert Data And Use Model, Resource Model, Collection

Magento, data interfaces, data models, and models.

We are talking about how to insert data and use of Model. Resource Model, Collection so obviously we need a table and custom module. I am assuming you have a custom module and if you don’t have click here to see how to create a simple custom module. Next thing is you must have a table in your database where you want to insert your data. So if you have a table created no problem. If you don’t have click here and create it first.

For this post, I am gonna use the simple custom module that we have created before in this post – here
And I am gonna use the table that we have created in this post – here

If all things are clear then let’s get started.
We have a Vky/Test/view/frontend/templates/test.phtml and in it, we just have below code right now.

To insert data we are going to use a form. I am just going to copy the default contact from design to creating a form. You can make your form however you want just make sure input names are the same as the database table field names. And I am not applying any validation to make it quick and short. I already have made a post about validation if you want to check that click here. And and I will post one more to cover validation in Magento 2.
So our edited Vky/Test/view/frontend/templates/test.phtml file should look like below now.

One thing you need to note is we have used action="<?php echo $block->getBaseUrl().'test/index/save'; ?>"

<?php echo $block->getBaseUrl(); ?> this is how we get base URL in our phtml file. After that
test – is our module index – is our controller and save – is our action which we don’t have yet. We have a index controller and index action in our custom module. So let’s create a save action for our index controller now.
Create Vky/Test/Controller/Index/Save.php

Now our form has the input names same as the database fields. So we can use $test->setData($data); otherwise we will have to use $test->setTitle("Some Title"); you can do this for other fields also if input names are not the same or you want to insert data into the field but not require to display in the form.

Use Model, Resource Model, Collection for the custom module?

Now to insert data using the Resource Model or use Model, Resource Model, Collection for the custom module we only need 3 files but in a proper folder structure.
First, Create Vky/Test/Model/Test.php

Second, Create Vky/Test/Model/ResourceModel/Test.php

Third, Create Vky/Test/Model/ResourceModel/Test/Collection.php

Now we have created a new action for our controller and a few more files to our custom module I suggest you to run some commands from your project’s root directory.

I think we are done here now. Open your URL where we are displaying our form. For example, it’s www.magento2.com/test. You will have your form there now submit the form data should be inserted in your custom table and you will be redirected to the same page with a success message.

How to get the collection?

We are going to get the collection and display that data in frontend in next post. But if you want it right now try this
$test = $this->_test->create(); // after this line
try below code.
echo '<pre>'; print_r($test->getCollection()->getData());die((__FILE__).'-->'.(__FUNCTION__).'--Line('. (__LINE__).')');

That’s how you insert data into the table using the Resource Model and use Model. Resource Model, Collection in Magento 2.
I hope it helps you.

Next: Display custom module collection data at frontend in Magento 2
(Check the post)

 

No Comments

Post A Comment