zf3自定义数据库工厂不希望应用


zf3 Custom database factory doesn't want to apply

数据库自定义工厂有奇怪的行为。

例如,我想使用BjyProfiler并创建一个这样的配置:

'db' => array(
    'driver' => 'Pdo',
    'dsn' => 'mysql:dbname=framework;host=localhost',
    'username' => 'root',
    'password' => '',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ''UTF8'''
    ),
),
'service_manager' => array(
    'factories' => array(
        'Zend'Db'Adapter'Adapter' => 'Database'Adapter'MainAdapterFactory',
    ),
),

所以要使用Zend'Db,我在modules.config.php"Zend'Db"中添加了模块(否则我会得到异常)。问题是,当我想获得"Zend'Db'Adapter'Adapter"时,它永远不会通过"Database'Adapter'MainAdapterFactory",我不知道为什么……它使用一些默认的适配器。我经常把工厂声明放在global.php, local.php中,但它不起作用。为什么会发生这种情况?在zf2中,这段代码是ok的…

如果需要的话,我使用composer

更新:在我的最终配置中,我有:

'service_manager' => 
    array (size=5)
      'aliases' => 
           array (size=11)
             ...
             'Zend'Db'Adapter'Adapter' => string 'Zend'Db'Adapter'AdapterInterface' (length=32)
             ...
      'factories' => 
           array (size=19)
             ...
             'Zend'Db'Adapter'AdapterInterface' => string 'Zend'Db'Adapter'AdapterServiceFactory' (length=37)
             ...
          'Zend'Db'Adapter'Adapter' => string 'Database'Adapter'Factory'MainAdapterFactory' (length=43)
      'abstract_factories' => 
           array (size=3)
             ...
             1 => string 'Zend'Db'Adapter'AdapterAbstractServiceFactory' (length=45)
             ...
   ...

我不知道别名'Zend'Db'Adapter'Adapter' => 'Zend'Db'Adapter'AdapterInterface'是从哪里来的,但我认为这就是问题所在。

'Zend'Db'Adapter'Adapter' => 'Zend'Db'Adapter'AdapterInterface'来自框架。

我建议您使用自己的适配器扩展原始'Zend'Db'Adapter'Adapter'(让我们称之为MyAdapter),并使用您的自定义适配器:

'service_manager' => array(
    'factories' => array(
        'Zend'Db'Adapter'MyAdapter' => 'Database'Adapter'MainAdapterFactory',
    ),
),