Answers:
这就是我设法做到的。不知道这是否是正确的方法,但是它有效(仅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');
}
}
应用/代码/供应商/模块/模型/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');
}
}
希望它可以帮助某人。
米歇尔