Database magento 2拆分数据库并使用新连接

Database magento 2拆分数据库并使用新连接,database,split,connection,magento2,Database,Split,Connection,Magento2,我已经为我的magento2网站实现了多个数据库。我一共有三个数据库。magento、magento_checkout和magento_quote。我已经创建了自定义模块,该模块在安装脚本中具有自定义平面表。当该模块运行时,它正在magento数据库中创建我的表。我想在magento_签出中创建它。我想使用结帐数据库的连接。我尝试在di.xml中设置argunment以使用签出数据库的连接,但它不起作用。有人能帮忙吗 名称空间=Exigo 模块名称=flipdf 数据库名称=测试 用户名=根 密

我已经为我的magento2网站实现了多个数据库。我一共有三个数据库。magento、magento_checkout和magento_quote。我已经创建了自定义模块,该模块在安装脚本中具有自定义平面表。当该模块运行时,它正在magento数据库中创建我的表。我想在magento_签出中创建它。我想使用结帐数据库的连接。我尝试在di.xml中设置argunment以使用签出数据库的连接,但它不起作用。有人能帮忙吗

名称空间=Exigo

模块名称=flipdf

数据库名称=测试

用户名=根

密码=根

1) 在app/etc/env.php中添加以下代码:

'custom' => array (
              'host' => 'localhost',
              'dbname' => 'test',
              'username' => 'root',
              'password' => 'root',
              'engine' => 'innodb',
              'initStatements' => 'SET NAMES utf8;',
              'active' => '1',
          ),
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Exigo\Flippdf\Model\ResourceModel\Flippdf">
        <arguments>
           <argument name="connectionName" xsi:type="string">custom_setup</argument>
        </arguments>
    </type>
</config>

最终的env.php文件如下所示:

  'db' => 
  array (
    'table_prefix' => '',
    'connection' => 
    array (
      'default' => 
      array (
        'host' => 'localhost',
        'dbname' => 'magento2',
        'username' => 'root',
        'password' => 'root',
        'active' => '1',
      ),
       'custom' => array (
          'host' => 'localhost',
          'dbname' => 'test',
          'username' => 'root',
          'password' => 'root',
          'engine' => 'innodb',
          'initStatements' => 'SET NAMES utf8;',
          'active' => '1',
      ),
    ),
  ),
  'resource' => 
  array (
    'default_setup' => 
    array (
      'connection' => 'default',
    ),
     'custom' => 
    array(
      'connection' => 'custom'
    ),
  ),
2) 下一步是配置资源模型以使用新连接。模块的di.xml配置文件将有助于为资源模型设置新的资源名称。在Exigo/flipdf/etc/di.xml中添加以下代码:

'custom' => array (
              'host' => 'localhost',
              'dbname' => 'test',
              'username' => 'root',
              'password' => 'root',
              'engine' => 'innodb',
              'initStatements' => 'SET NAMES utf8;',
              'active' => '1',
          ),
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Exigo\Flippdf\Model\ResourceModel\Flippdf">
        <arguments>
           <argument name="connectionName" xsi:type="string">custom_setup</argument>
        </arguments>
    </type>
</config>

自定义设置
3) 现在,您可以按如下方式打印外部数据库数据:

  'db' => 
  array (
    'table_prefix' => '',
    'connection' => 
    array (
      'default' => 
      array (
        'host' => 'localhost',
        'dbname' => 'magento2',
        'username' => 'root',
        'password' => 'root',
        'active' => '1',
      ),
       'custom' => array (
          'host' => 'localhost',
          'dbname' => 'test',
          'username' => 'root',
          'password' => 'root',
          'engine' => 'innodb',
          'initStatements' => 'SET NAMES utf8;',
          'active' => '1',
      ),
    ),
  ),
  'resource' => 
  array (
    'default_setup' => 
    array (
      'connection' => 'default',
    ),
     'custom' => 
    array(
      'connection' => 'custom'
    ),
  ),
$collection=$this->_modelFlipDFfactory->create()

$item=$collection->getCollection()

打印($item->getData())

例如,在控制器文件中:

<?php

namespace Exigo\Flippdf\Controller\Index;

use Exigo\Flippdf\Model\FlippdfFactory;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    protected $_modelFlippdfFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
         FlippdfFactory $modelFlippdfFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_modelFlippdfFactory = $modelFlippdfFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $collection = $this->_modelFlippdfFactory->create();
        $item = $collection->getCollection();

        echo "<pre>";
        print_r($item->getData());// Get test table data from external database
        exit;

        //return $this->resultPageFactory->create();
    }
}

名称空间=Exigo

模块名称=flipdf

数据库名称=测试

用户名=根

密码=根

1) 在app/etc/env.php中添加以下代码:

'custom' => array (
              'host' => 'localhost',
              'dbname' => 'test',
              'username' => 'root',
              'password' => 'root',
              'engine' => 'innodb',
              'initStatements' => 'SET NAMES utf8;',
              'active' => '1',
          ),
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Exigo\Flippdf\Model\ResourceModel\Flippdf">
        <arguments>
           <argument name="connectionName" xsi:type="string">custom_setup</argument>
        </arguments>
    </type>
</config>

最终的env.php文件如下所示:

  'db' => 
  array (
    'table_prefix' => '',
    'connection' => 
    array (
      'default' => 
      array (
        'host' => 'localhost',
        'dbname' => 'magento2',
        'username' => 'root',
        'password' => 'root',
        'active' => '1',
      ),
       'custom' => array (
          'host' => 'localhost',
          'dbname' => 'test',
          'username' => 'root',
          'password' => 'root',
          'engine' => 'innodb',
          'initStatements' => 'SET NAMES utf8;',
          'active' => '1',
      ),
    ),
  ),
  'resource' => 
  array (
    'default_setup' => 
    array (
      'connection' => 'default',
    ),
     'custom' => 
    array(
      'connection' => 'custom'
    ),
  ),
2) 下一步是配置资源模型以使用新连接。模块的di.xml配置文件将有助于为资源模型设置新的资源名称。在Exigo/flipdf/etc/di.xml中添加以下代码:

'custom' => array (
              'host' => 'localhost',
              'dbname' => 'test',
              'username' => 'root',
              'password' => 'root',
              'engine' => 'innodb',
              'initStatements' => 'SET NAMES utf8;',
              'active' => '1',
          ),
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Exigo\Flippdf\Model\ResourceModel\Flippdf">
        <arguments>
           <argument name="connectionName" xsi:type="string">custom_setup</argument>
        </arguments>
    </type>
</config>

自定义设置
3) 现在,您可以按如下方式打印外部数据库数据:

  'db' => 
  array (
    'table_prefix' => '',
    'connection' => 
    array (
      'default' => 
      array (
        'host' => 'localhost',
        'dbname' => 'magento2',
        'username' => 'root',
        'password' => 'root',
        'active' => '1',
      ),
       'custom' => array (
          'host' => 'localhost',
          'dbname' => 'test',
          'username' => 'root',
          'password' => 'root',
          'engine' => 'innodb',
          'initStatements' => 'SET NAMES utf8;',
          'active' => '1',
      ),
    ),
  ),
  'resource' => 
  array (
    'default_setup' => 
    array (
      'connection' => 'default',
    ),
     'custom' => 
    array(
      'connection' => 'custom'
    ),
  ),
$collection=$this->_modelFlipDFfactory->create()

$item=$collection->getCollection()

打印($item->getData())

例如,在控制器文件中:

<?php

namespace Exigo\Flippdf\Controller\Index;

use Exigo\Flippdf\Model\FlippdfFactory;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    protected $_modelFlippdfFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
         FlippdfFactory $modelFlippdfFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_modelFlippdfFactory = $modelFlippdfFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $collection = $this->_modelFlippdfFactory->create();
        $item = $collection->getCollection();

        echo "<pre>";
        print_r($item->getData());// Get test table data from external database
        exit;

        //return $this->resultPageFactory->create();
    }
}