Magento 2 - read and write connection for custom queryMagento get read or write connection for model / resource modelget generated Id from custom write connectionMagento 2 Customer Order Attribute SQL Error in ControllerMagento Read & Write Connection Close Manually NeededHow to write mysql update query in magento 2 format?Custom Connection to databaseHow and where to add join query in magento 2 for UI componentssql query in cms_block table in magento 2Magento 2 master slave connection for DBSQL Query for custom attribute

VHDL: Why is it hard to design a floating point unit in hardware?

Department head said that group project may be rejected. How to mitigate?

amsmath: How can I use the equation numbering and label manually and anywhere?

Meaning of "half-crown enclosure"

What is the required burn to keep a satellite at a Lagrangian point?

Why is unzipped file smaller than zipped file

Why do the i8080 I/O instructions take a byte-sized operand to determine the port?

Is it normal to "extract a paper" from a master thesis?

Why is this integration method not valid?

How would a physicist explain this starship engine?

How to become an Editorial board member?

Is the default 512 byte physical sector size appropriate for SSD disks under Linux?

Keeping the dodos out of the field

(For training purposes) Are there any openings with rook pawns that are more effective than others (and if so, what are they)?

How many wires should be in a new thermostat cable?

What is the winged creature on the back of the Mordenkainen's Tome of Foes book?

Can someone get a spouse off a deed that never lived together and was incarcerated?

Team member is vehemently against code formatting

What was the primary motivation for a historical figure like Xenophon to create an extensive collection of written material?

Why is the reciprocal used in fraction division?

What defines a person who is circumcised "of the heart"?

nginx conf: http2 module not working in Chrome in ubuntu 18.04

How to test if argument is a single space?

Is there an idiom that means that you are in a very strong negotiation position in a negotiation?



Magento 2 - read and write connection for custom query


Magento get read or write connection for model / resource modelget generated Id from custom write connectionMagento 2 Customer Order Attribute SQL Error in ControllerMagento Read & Write Connection Close Manually NeededHow to write mysql update query in magento 2 format?Custom Connection to databaseHow and where to add join query in magento 2 for UI componentssql query in cms_block table in magento 2Magento 2 master slave connection for DBSQL Query for custom attribute






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I am using Magento 2.2.5 EE. I need to write some custom queries Select and Insert



I know how to write custom query, its simple:



Create new connection:



$objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
$connection = $resource->getConnection();


Apply select query through this:



$tableName = $resource->getTableName('employee'); //gives table name with prefix
//Select Data from table
$sql = "Select * FROM " . $tableName;
$result = $connection->fetchAll($sql);


And Insert command using this:



//Insert Data into table
$sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')";
$connection->query($sql);


In magento 1.9, we just define "core_read" and "core_write" for our custom queries, it was that simple.



My question is, how Magento knows when to insert on master DB (write) and when to get data from read replica (read)? I mean the object for both commands is same.










share|improve this question






























    2















    I am using Magento 2.2.5 EE. I need to write some custom queries Select and Insert



    I know how to write custom query, its simple:



    Create new connection:



    $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of object manager
    $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
    $connection = $resource->getConnection();


    Apply select query through this:



    $tableName = $resource->getTableName('employee'); //gives table name with prefix
    //Select Data from table
    $sql = "Select * FROM " . $tableName;
    $result = $connection->fetchAll($sql);


    And Insert command using this:



    //Insert Data into table
    $sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')";
    $connection->query($sql);


    In magento 1.9, we just define "core_read" and "core_write" for our custom queries, it was that simple.



    My question is, how Magento knows when to insert on master DB (write) and when to get data from read replica (read)? I mean the object for both commands is same.










    share|improve this question


























      2












      2








      2








      I am using Magento 2.2.5 EE. I need to write some custom queries Select and Insert



      I know how to write custom query, its simple:



      Create new connection:



      $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of object manager
      $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
      $connection = $resource->getConnection();


      Apply select query through this:



      $tableName = $resource->getTableName('employee'); //gives table name with prefix
      //Select Data from table
      $sql = "Select * FROM " . $tableName;
      $result = $connection->fetchAll($sql);


      And Insert command using this:



      //Insert Data into table
      $sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')";
      $connection->query($sql);


      In magento 1.9, we just define "core_read" and "core_write" for our custom queries, it was that simple.



      My question is, how Magento knows when to insert on master DB (write) and when to get data from read replica (read)? I mean the object for both commands is same.










      share|improve this question
















      I am using Magento 2.2.5 EE. I need to write some custom queries Select and Insert



      I know how to write custom query, its simple:



      Create new connection:



      $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of object manager
      $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
      $connection = $resource->getConnection();


      Apply select query through this:



      $tableName = $resource->getTableName('employee'); //gives table name with prefix
      //Select Data from table
      $sql = "Select * FROM " . $tableName;
      $result = $connection->fetchAll($sql);


      And Insert command using this:



      //Insert Data into table
      $sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')";
      $connection->query($sql);


      In magento 1.9, we just define "core_read" and "core_write" for our custom queries, it was that simple.



      My question is, how Magento knows when to insert on master DB (write) and when to get data from read replica (read)? I mean the object for both commands is same.







      database magento-2.2.5 database-connection read-write






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 15 at 7:44







      Shoaib Munir

















      asked Feb 25 at 6:09









      Shoaib MunirShoaib Munir

      3,08151760




      3,08151760




















          1 Answer
          1






          active

          oldest

          votes


















          0














          First you should update your question so that one can find that you are talking about multiple db configuration.



          You can't use direct queries for this type of configuration. As it is stated in documentation.Split database performance solution (Magento Commerce only)




          Configuration options
          Because of the way the split database performance solution is designed, your custom code and installed components cannot do any of the following:




          - Write directly to the database (instead, you must use the Magento Commerce database interface)
          - Use JOINs that affect the sales or quote databases
          - Use foreign keys to tables in the checkout, sales, or main databases





          share|improve this answer























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263222%2fmagento-2-read-and-write-connection-for-custom-query%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            First you should update your question so that one can find that you are talking about multiple db configuration.



            You can't use direct queries for this type of configuration. As it is stated in documentation.Split database performance solution (Magento Commerce only)




            Configuration options
            Because of the way the split database performance solution is designed, your custom code and installed components cannot do any of the following:




            - Write directly to the database (instead, you must use the Magento Commerce database interface)
            - Use JOINs that affect the sales or quote databases
            - Use foreign keys to tables in the checkout, sales, or main databases





            share|improve this answer



























              0














              First you should update your question so that one can find that you are talking about multiple db configuration.



              You can't use direct queries for this type of configuration. As it is stated in documentation.Split database performance solution (Magento Commerce only)




              Configuration options
              Because of the way the split database performance solution is designed, your custom code and installed components cannot do any of the following:




              - Write directly to the database (instead, you must use the Magento Commerce database interface)
              - Use JOINs that affect the sales or quote databases
              - Use foreign keys to tables in the checkout, sales, or main databases





              share|improve this answer

























                0












                0








                0







                First you should update your question so that one can find that you are talking about multiple db configuration.



                You can't use direct queries for this type of configuration. As it is stated in documentation.Split database performance solution (Magento Commerce only)




                Configuration options
                Because of the way the split database performance solution is designed, your custom code and installed components cannot do any of the following:




                - Write directly to the database (instead, you must use the Magento Commerce database interface)
                - Use JOINs that affect the sales or quote databases
                - Use foreign keys to tables in the checkout, sales, or main databases





                share|improve this answer













                First you should update your question so that one can find that you are talking about multiple db configuration.



                You can't use direct queries for this type of configuration. As it is stated in documentation.Split database performance solution (Magento Commerce only)




                Configuration options
                Because of the way the split database performance solution is designed, your custom code and installed components cannot do any of the following:




                - Write directly to the database (instead, you must use the Magento Commerce database interface)
                - Use JOINs that affect the sales or quote databases
                - Use foreign keys to tables in the checkout, sales, or main databases






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 26 at 14:32









                Knight017Knight017

                387212




                387212



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263222%2fmagento-2-read-and-write-connection-for-custom-query%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form