doushan9415 2016-11-10 20:28
浏览 57

如何获取数据库适配器的配置

I'm using more than one database. So I have this kind of configuration in my global.php file:

return [
'db' => [
    ...
    ],
    'adapters' => [
        'adapter1' => [
            ...
            ]
        ],
        'adapter2' => [
            ...
            ]
        ]
    ]
]
];

And my goal is to get the configuration of all my adapters in one instruction. I don't know if it is possible. There is this instruction to get one adapter :

$sm->get('adapter1');

But I want to have something like that:

$sm->get('adapters');

I didn't find any solution right now so I decided to ask the question here... I suppose this is something possible because the file just containing a simple array...

Yours

EDIT :

I tried

$sm->get('config')['db']['adapters'];

But it returns an array of string. Actually, I would like an array of Objects. Objects of Adapter. The same way I get an adapter in this way :

$sm->get('adapter1');

Yours

  • 写回答

2条回答 默认 最新

  • douyanpeng0748 2016-11-11 10:47
    关注

    Not sure if this is what you are looking for but you could use Zend\Config class for this. You can find more information on this class here in the documentation:

    // Create the object-oriented wrapper using the configuration data
    $adaptersConfig = $sm->get('config')['db']['adapters'];
    $adapters = new \Zend\Config($adaptersConfig);
    

    Now you can access adapters like you wanted:

    $adapters->adapter1;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图