top of page
  • Writer's picturenevinainfotech

Understanding The External Database Connection in Magento


Most of the time, a unary database connection is sufficient for working in Magento. Although Magento supports the addition of new tables or extension of existing tables in the database, online stores, without technical expertise, generally hire magento development company in india for this kind of basic stuff also. Imagine what would happen if they require connecting to an external database outside the purview of Magento. This kind of scenario can occur while migrating from one eCommerce system to another. Let us go through the process of performing CRUD operations on the external database through Magento.

Understanding env.php

Once Magento 2 is successfully installed, it builds a configuration file, i.e., app/etc/env.php. This file is used to hoard important information like the password of the database, key for encryption. The following connection information of the database is stored in this important file.


'db' =>

array (

'table_prefix' => ' ',

'connection' =>

array (

'default' =>

array (

'host' => 'localhost1',

'dbname' => 'magento2_0',

'username' => 'magento2_user',

'password' => 'magento2_password',

'model' => 'mysql4_db',

'engine' => 'inno_db',

'initStatements' => 'SET NAMES utf8;',

'active' => '1',

)

),

),


The entire configuration information related to database is stored under the db key of main configuration. Let us try to understand these database settings.


  • table_prefix: Stores the table prefixes of all the installed database tables.

  • connection: Stores all accessible configuration for database connections.

  • default: Stores the database connection name.

  • host: Stores the database host name.

  • dbname: Stores the name of the database.

  • username: Stores the user name for the database connection.

  • password: Stores the password of database user.

  • model: Stores the type of database.

  • engine: Stores the name of the database engine.

  • initStatements: Stores the initial statements supposed to execute while establishing connection with the database.

  • active: Specifies whether connection is available

Creating a new Database Connection

The following changes must be done to the env.php file to create a new custom connection. Ensure that the connection name is unique to avoid any kind of conflict.

'custom' => array (

'host' => 'localhost1',

'dbname' => 'CustomDatabase',

'username' => 'CustomUser',

'password' => 'CustomPassword',

'engine' => 'inno_db',

'initStatements' => 'SET NAMES utf8;',

'active' => '1',

)

Apart from this, fresh resource settings need to be included to assign custom connection with resource.

'resource' => array (

'custom' => array(

'connection' => 'custom'

)

)

It is due to these technicalities that online businesses hire magento developers to modify the database and resource configuration settings.

XML Configuration

Now, the subsequent step is to configure the resource model to use the new database connection. For this task, the configuration file di.xml is used. It sets up a new resource name for the resource model.

<?xml version="1.0"?>

<config xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<type name="Nevina\CustomModule\Model\ResourceModel\Blogpost">

<arguments>

<argument name="connectionName" xsi:type="string">CustomSetup</argument>

</arguments>

</type>

</config>

It can be seen here that the CustomSetup argument has been supplied to the ConnectionName argument, whereas, in the env.php file, the resource names used were default and custom. It is not mandatory to have a suffix _setup. The getConnectionName() method in TheMagento\Framework\App\ResourceConnection\Config class uses preg_replace function (It performs regular expression for searching and replacing the content) to chuck out _setup.


public function getConnectionName($resourceName)

{

$connectionName = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION;

$resourceName = preg_replace("/_setup$/", '', $resourceName);

// additional code ...

}

The Nevina\CustomModule\Model\ResourceModel\Blogpost instance uses connectionName to set up the actual database connection to be used in the class. Upon usage of getConnection() inside the class, a custom connection is returned with another database.


Final Remarks

There are several potent extension means to adjoin a fresh database connection. It is helpful while constructing the server infrastructure of scalable nature especially, when the read operation and write operations split among the databases. Non-technical businesses hire magento development company in india to migrate from an eCommerce system to another wherein external database connection is required. The steps mentioned here are useful for businesses looking for connecting to an exterior database for the necessity of any kind – rare or routine. It is not required to hire magento developers for performing elementary database operations on an external database upon following these steps.


85 views0 comments
bottom of page