Sometimes we need to write custom insert query in magento2 as per business logic or client requirement.

In this blog, We will create some standard code to insert data using custom insert.

We need to use magento ResourceConnection class for insert data in mysql table.

Here we are adding a new record in custom table call_logs, that contains three columns only id, external_number and duration

PHP
<?php
declare(strict_types=1);

namespace MageDad\Module\Model;

use Magento\Framework\App\ResourceConnection;

class CallLogManagement
{
    public function __construct(
        private ResourceConnection $resourceConnection,
    ) { }
    
    public function saveCallLog()
    {
        $data = [
            'external_number' => '09876543210',
            'duration' => 100
        ];
        
        $connection  = $this->resourceConnection->getConnection();
        $tableName = $connection->getTableName('call_logs');
        $connection->insert($tableName, $data);
        
        
        $data = [
            [
                'external_number' => '09876543210',
                'duration' => 100
            ],
            [
                'external_number' => '09876543211',
                'duration' => 200
            ]
        ];
        $connection->insertMultiple($tableName, $data);
    }
}

Above code in we used insert function for single record call_logs table. We have function insertMultiple for more then one record.

I hope this blog is useful to custom insert query in magento2. In case, I missed anything or need to add some more information, Don’t heisted to leave a comment in this blog, I’ll get back with some positive approach.

Keep loving ❤️ Keep inspiring 🤩 Keep liking 👍 No sharing 😄

17 Comments

  1. Hi there just wanted to give you a quick heads up. The words in your article seem to be running off the screen in Ie. I’m not sure if this is a format issue or something to do with browser compatibility but I thought I’d post to let you know. The design and style look great though! Hope you get the issue fixed soon. Kudos

  2. User-Friendly Interface and Accessibility Businessiraq.com is designed with a focus on user experience, ensuring that visitors can easily navigate the site to find the information they need. With a clean layout and intuitive search functions, users can swiftly access the Iraq Business Directory, job listings, and the latest business news. This commitment to user-friendly design not only enhances engagement but also contributes positively to SEO, as search engines prioritize websites that provide a seamless browsing experience. By making essential business information easily accessible, Businessiraq.com positions itself as the premier online resource for anyone looking to thrive in the Iraqi business environment.

  3. Neu anh em dang tim kiem mot nha cai truc tuyen uy tin de tham gia ca cuoc, thi xo88chinh la lua chon ly tuong. Day la nen tang ca cuoc hang dau mang den trai nghiem chuyen nghiep va chat luong cao voi hang loat tro choi da dang, hap dan. xo88.name

  4. Excellent beat ! I wish to apprentice at the same time as you amend your website, how could i subscribe for a blog web site? The account helped me a appropriate deal. I have been a little bit acquainted of this your broadcast provided bright transparent idea

  5. Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.

  6. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

  7. Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  8. 이러한 유형의 주제를 검색하는 거의 모든 사람들에게 도움이되기를 바랍니다. 저는이 웹 사이트가 그러한 주제에 가장 적합하다고 생각합니다. 좋은 작품과 기사의 품질. 벳톡카지노

  9. That is really fascinating, You’re an overly professional blogger. I have joined your feed and look forward to seeking more of your magnificent post. Additionally, I’ve shared your site in my social networks!

  10. Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

  11. Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  12. Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

Write A Comment