ZF2 重新启动服务管理器密钥 [英] ZF2 Re-starting service manager key

查看:25
本文介绍了ZF2 重新启动服务管理器密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务管理器中存储了一个名为dbAdapter"的数据库适配器.我正在寻找一种方法来断开连接,然后使用稍微不同的配置重新连接到此适配器,可能多次.

I have a database adapter stored in my service manager called "dbAdapter". I am looking for a way to disconnect and then re-connect to this adapter using a slightly different configuration, possibly several times.

原因是我有很多客户的数据库结构是 100% 相同的.我正在编写一个 cron 作业,该作业将对每个数据库进行维护,但它必须连接到第一个数据库,执行它的操作,断开连接并连接到下一个......直到它用完数据库.

The reason is that I have many customers who's database structure is 100% is the same. I am writing a cron job that will do maintance on each database but it has to connect to the first one, do its thing, drop the connection and connect to the next one...until it is out of databases.

我正在查看源代码,但我只能在 Zend\ServiceManager\ServiceManager.php 中找到一个名为 unregisterService 的受保护方法,它看起来像我想要的那样,但它是一个受保护的方法,因此我无法调用它来自我的控制器.

I was looking through the source but I could only find a single protected method in Zend\ServiceManager\ServiceManager.php called unregisterService which looks like it would do what I want but it is a protected method and thus I can't call it from my controller.

是否可以过期"服务管理器密钥并强制它在下次调用时重新创建它?

Is it possible to "expire" a service manager key and force it to re-create it on its next call?

推荐答案

您可以在 ServiceManager 中将 $allowOverride 设置为 true 然后在名称 dbAdapter 下注册一个 null 值.

You could set $allowOverride to true in the ServiceManager and then register a null value under the name dbAdapter.

$serviceManager->setAllowOverride(true)
               ->setService('dbAdapter', null)
               ->setAllowOverride(false);

这篇关于ZF2 重新启动服务管理器密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆