4

I originally made another thread about this a couple of months ago in regards to ZF2 injecting into tables with DI during Beta 1 and figured back then that it wasn't really possible. Now ZF2 has been released as version 2.0.0 and ServiceManager is defaulted to instead of DI I guess I have the same question now I'm refactoring.

I've got 84 tables that need the DbAdapter injecting into them and I'm sure there has to be a better way as I'm replicating myself SO much.

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'accountTable' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $table     = new Model\DbTable\AccountTable($dbAdapter);
                return $table;
            },
            'userTable' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $table     = new Model\DbTable\UserTable($dbAdapter);
                return $table;
            },
            // another 82 tables of the above
        )
    )
}

With the EventsManager and ServiceManager I have no idea where I stand in getting my application's instances/resources.

Thanks, Dom

4

3 回答 3

2

有了很多这样的注入,你最好创建一个服务管理器初始化器,然后实现一个像 DbAdapterAwareInterface 这样的接口。如果您想了解这个想法的实际效果,请查看 zf2 代码库中的 EventManagerAwareInterface。

于 2012-09-19T23:50:58.577 回答
0

我编写了一个名为DiWrapper的 ZF2 模块,您可以使用它来自动生成和自动更新这种工厂代码。您必须将Zend\Db\Adapter\AdapterDiWrapper 作为共享实例提供。然后您可以使用 ServiceManager 或 DiWrapper 来获取您的表类。

于 2013-03-09T20:50:54.627 回答
-1

它真的可以。php5.3+ 仅注入 *pointers 或“链接”

于 2012-09-20T03:36:25.263 回答