Search code examples
phpzend-frameworkzend-framework3zf3

zf3 Custom database factory doesn't want to apply


I have strange behavior with database custom factory.

For example I want to use BjyProfiler and create 1 config like this:

'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',
    ),
),

So to use Zend\Db I added the module in modules.config.php "Zend\Db"(otherwise I get exceptions). The problem is that when I want to get "Zend\Db\Adapter\Adapter", it never go through "Database\Adapter\MainAdapterFactory" and I don't know why... It use some default Adapter. I triend to put factory declaration in global.php, local.php and it doesn't work. Why is this happening? In zf2 this code is ok...

I use composer if that matters.

Update: In my final config I have:

'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)
             ...
   ...

I don't know from where it comes alias 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterInterface' but I think this is the problem.


Solution

  • The 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterInterface' come from the framework.

    I suggested you to extends the original 'Zend\Db\Adapter\Adapter' with your own adapter (let's call it MyAdapter), and use your custom adapter:

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