2016-08-28 9 views
1

私はmagento2ウェブサイト用に複数のデータベースを実装しています。私は合計3つのデータベースを持っています..magento、magento_checkout、magento_quote。私は、カスタムスクリプトモジュールを作成しました。このモジュールを実行すると、magentoデータベースにテーブルが作成されています。私はそれをmagento_checkoutで作成したい。私はチェックアウトデータベースの接続を使いたいです。私はチェックアウトデータベースの接続を使用するdi.xmlにargunmentを設定しようとしていますが、動作しません。誰かがこれで助けることができますか?magento 2分割データベースと新しい接続を使用

答えて

0

名前空間= EXIGO

モジュール名= Flippdf

データベース名=テスト

ユーザ名=ルート

パスワード=ルート

1)アプリで/ etc/env.phpこのコードを追加します。

'custom' => array (
       'host' => 'localhost', 
       'dbname' => 'test', 
       'username' => 'root', 
       'password' => 'root', 
       'engine' => 'innodb', 
       'initStatements' => 'SET NAMES utf8;', 
       'active' => '1', 
     ), 

そして

'custom' => 
array(
    'connection' => 'custom' 
), 

最終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で/ Flippdfの/ etc /このコードを追加しますdi.xml:今、あなたは、このように外部データベースのデータを印刷することができます)

<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:

$コレクション= $この - > _ modelFlippdfFactory->作成();

$ item = $ collection-> getCollection();

print_r($ 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(); 
    } 
} 
:コントローラファイル、例えば

関連する問題