때로는 다른 데이터베이스, Mysql / Oracle 등에 연결해야합니다. Magento2에서 가장 좋은 방법은 무엇입니까?
때로는 다른 데이터베이스, Mysql / Oracle 등에 연결해야합니다. Magento2에서 가장 좋은 방법은 무엇입니까?
답변:
이것은 공식적으로 지원되지 않습니다. 호출자가 서비스 계약을 통해 다시 구현해야하는 API (기본 구현은 di.xml 파일을 사용하여 대체 할 수 있음)를 제한하기 위해 더 나은 모듈화로 나아가고 있지만 Magento 2.0.0에서는 간단한 작업이 아닙니다. . 우리는 그런 식으로 움직이고 있지만 "쉬운"할 때 ETA는 없습니다.
예를 들어, 플러그인을 사용하여 데이터베이스 호출 또는 모듈 호출을 가로 챌 수 있습니다. di.xml을 사용하여 기본 구현을 바꿀 수 있습니다. 등등. 그렇게하는 방법이 있습니다. (사람들은 때때로 M1에서 이것을한다.)
이것이 내가 관리하는 방법입니다. 올바른 방법인지 모르겠지만 작동합니다 (mysql 만 해당).
app / etc / env.php
...
'db' =>
array (
'table_prefix' => '',
'connection' =>
array (
'default' =>
array (
'host' => 'localhost',
'dbname' => 'xxxx',
'username' => 'yyyy',
'password' => 'zzzz',
'active' => '1',
),
'myconnection' =>
array (
'host' => 'localhost',
'dbname' => 'somedbname',
'username' => 'xxxx',
'password' => 'yyyy',
'active' => '1',
),
),
),
'resource' =>
array (
'default_setup' =>
array (
'connection' => 'default',
),
'myconnection' =>
array (
'connection' => 'myconnection',
),
),
...
앱 / 코드 / 공급 업체 / 모듈 / 모델 /Test.php
<?php
namespace Vendor\Module\Model;
use \Magento\Framework\Model\AbstractModel;
class Page extends AbstractModel
{
const UID = 'uid';
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'test'; // parent value is 'core_abstract'
/**
* Name of the event object
*
* @var string
*/
protected $_eventObject = 'test'; // parent value is 'object'
/**
* Name of object id field
*
* @var string
*/
protected $_idFieldName = self::UID; // parent value is 'id'
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Vendor\Module\Model\ResourceModel\Page');
}
}
앱 / 코드 / 공급 업체 / 모듈 / 모델 /ResourceModel/Test.php
<?php
namespace Vendor\Module\Model\ResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Test extends AbstractDb
{
protected $connectionName = 'myconnection';
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
// Table Name and Primary Key column
$this->_init('testtable', 'uid');
}
}
app / code / Vendor / Module / Model / ResourceModel / Test / Collection.php
<?php
namespace Vendor\Module\Model\ResourceModel\Test;
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
protected $_idFieldName = \Vendor\Module\Model\Test::UID;
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Vendor\Module\Model\Test', 'Vendor\Module\Model\ResourceModel\Test');
}
}
그것이 누군가를 돕기를 바랍니다.
미셸