Doctrine with zf3 cache

Martin Zellerdoctrine, php, zf3 Leave a Comment

That’s the way how to use Zend Cache with Doctrine in Zend Framework 3.

First do all doctrine settings and define a cache in your global.php:

'doctrine' => [
    'connection' => [
        // default connection name
        'orm_default' => [
            'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
            'params' => [
                'host' => 'localhost',
                'port' => 3306,
                'user' => 'xxx',
                'password' => 'xxx',
                'dbname' => 'xxx',
                'charset' => 'utf8'
            ]
        ]
    ],
    'configuration' => [
        'orm_default' => [
            'metadata_cache'   => 'zend.dbcache',
            'query_cache'     => 'zend.dbcache',
            'result_cache'    => 'zend.dbcache',
        ]
    ],
],

'caches' => [
    'zend.dbcache' => [
        'adapter' => [
            'name' => 'filesystem',
            'options' => [
                'dirLevel' => 2,
                'cacheDir' => './data/cache/db',
                'dirPermission' => 0775,
                'filePermission' => 0666,
                'namespaceSeparator' => '-',
                'keyPattern' => '/^[\[\]\$\\a-z0-9_\+\-]*$/Di',
            ],
        ],
        'plugins' => [
            'exception_handler' => [
                'throw_exceptions' => false
            ],
            'serializer'
        ]
    ],

],

 

Then define a service ‘doctrine.cache.zend.dbcache‘ in your module/Application/src/Module.php:

/**
 * Expected to return \Zend\ServiceManager\Config object or array to
 * seed such an object.
 *
 * @return array|\Zend\ServiceManager\Config
 */
public function getServiceConfig()
{
    return [
        'factories' => [
            'doctrine.cache.zend.dbcache' => function(ServiceManager $serviceManager) {
                return new \DoctrineModule\Cache\ZendStorageCache($serviceManager->get('zend.dbcache'));
            }
        ],
    ];
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.