Doctrine caching configuration - php

In the doctrine docs, it looks like I need the configuration object available for metadata caching:
<?php
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
use Doctrine\Common\Proxy\ProxyFactory;
// ...
if ($applicationMode == "development") {
$cache = new \Doctrine\Common\Cache\ArrayCache;
} else {
$cache = new \Doctrine\Common\Cache\ApcCache;
}
$config = new Configuration;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver('/path/to/lib/MyProject/Entities');
$config->setMetadataDriverImpl($driverImpl);
However, this is currently inside a module in the zend directory in composer, so I cant change it. Id like to hand the option to do this in the array configuration:
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'params' => array(
'pdo' => new PDO("connection details")
),
'query_cache'=>new PhpFileCache(), //<- like this
'metadata_cache'=>new PhpFileCache(), //<- and this
),
),
),
I know this is possible because I have done it once before, however, I cant seem to find this method on the docs anymore. The current docs show the setup for YAML.

I can set up many connections in the Doctrine entity manager, but I supply one configuration since this sets up things that run once for the lifetime of that run, or if we have a cache installed such as APC, the lifetime of that release.
I achieve this by adding the info to $config['doctrine']['configuration'], not $config['doctrine']['orm_default/your_connection']['configuration']
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'params' => array(
'pdo' => new PDO("connection")
),
),
),
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'filesystem',
'query_cache' => 'filesystem',
'result_cache' => 'filesystem'
),
),
),

Related

zend framework 2 - Cache config files

i'm trying to enable the cache for the config files in zend framework 2 :
the module.config.php ( part of services ) :
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
'doctrine.cache.mycache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
),
),
the application.config.php ( part of enabling the cache for config ):
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor',
),
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'config_cache_enabled' => true,
'config_cache_key' => md5('config'),
'module_map_cache_enabled' => true,
'module_map_cache_key' => md5('module_map'),
'cache_dir' => "./data/cache/modulecache",
),
And here the error i got :
Fatal error: Call to undefined method Closure::__set_state()
Thanks.
Config files can't be cached if they contain anonymous functions (in your case, the value for doctrine.cache.mycache). You will need to move just that part out of the config file and into your Module.php class' getServiceConfig() instead. That should fix the issue.

How to store queries and their results in memcache using cache enabled for doctrine 2 (using zend framework 2 )?

I guess all is in the title , I've looked a lot for a solution but can't figure out how to do it ? Here is my configuration for memcache in module.config.php :
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
)
),
/***** enabling the memcache ****/
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'mycache',
'query_cache' => 'mycache',
'result_cache' => 'mycache',
)
/**** end ****/
)
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
'doctrine.cache.mycache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
),
),
i'm using zend framework 2 and doctrine 2 .
thanks.
If everything is configured correctly you don't need to configure anything else for Query Cache and Metadata Cache to work, however, to enable Result Cache you will have to call useResultCache explicitly on each query.
Example:
<?php
$query = $em->createQuery('select u from \Entities\User u');
$query->useResultCache(true);

Enable Doctrine 2 cache in a ZF2 project

How to enable cache in a project working with Zend Framework 2 and Doctrine 2? and what cache exactly should be enabled the doctrine cache or the zend cache?
Here what i've tried but can't see any diference in the time execution added in the
module\Application\config\module.config.php
'doctrine.cache.my_memcache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
'doctrine.cache.apc' => function ($sm){
$apc = new \Doctrine\Common\Cache\ApcCache();
return $apc;
},
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'),
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
)
),
'configuration' => array(
'orm_defaults' => array(
'metadata_cache' => 'apc',
'query_cache' => 'apc',
'result_cache' => 'my_memcache',
)
)
),
any help or idea or explication is appreciated.
thanks.
To reduce unnecessary headaches, always use array cache on development time and memcached, redis or apc when your application running on production environment.
You should put your factory definitions under the service_manager > factories key, not directly in the module configuration array.
Try this in your module.config.php:
return [
'doctrine' => [
'configuration' => [
'orm_default' => [
'metadata_cache' => 'mycache',
'query_cache' => 'mycache',
'result_cache' => 'mycache',
'hydration_cache' => 'mycache',
]
],
],
'service_manager' => [
'factories' => [
'doctrine.cache.mycache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
],
],
];
Also I strongly recommend moving factories to individual factory classes, always. This way, you'll have a more readable, maintainable and efficient application on production environment with the help of merged configuration cache.
For example:
'service_manager' => [
'factories' => [
'doctrine.cache.mycache' => \App\Memcache\Factory::class // implement FactoryInterface
],
],
];
Update for future readers after several years:
I would highly recommend looking into roave/psr-container-doctrine
before writing custom dedicated factories for various doctrine components such as entity manager, config or cache. As a contributor of the library I can say that it serves the purpose nicely for most of the use cases I needed so far. When you really need a specialized factory, you can either extend or compose or decorate factories provided and add your own logic on top it.

How can i configure Bjyprofiler for Doctrine2 in Zend Framework?

Help , plz how can i configure Bjyprofiler for doctrine2 ?
all the configurations i found is about pdo , i' m working with zend framework 2 and doctrine2.
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => function ($sm) use ($dbParams) {
$adapter = new BjyProfiler\Db\Adapter\ProfilingAdapter(array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname='.$dbParams['database'].';host='.$dbParams['hostname'],
'database' => $dbParams['database'],
'username' => $dbParams['username'],
'password' => $dbParams['password'],
'hostname' => $dbParams['hostname'],
));
if (php_sapi_name() == 'cli') {
$logger = new Zend\Log\Logger();
// write queries profiling info to stdout in CLI mode
$writer = new Zend\Log\Writer\Stream('php://output');
$logger->addWriter($writer, Zend\Log\Logger::DEBUG);
$adapter->setProfiler(new BjyProfiler\Db\Profiler\LoggingProfiler($logger));
} else {
$adapter->setProfiler(new BjyProfiler\Db\Profiler\Profiler());
}
if (isset($dbParams['options']) && is_array($dbParams['options'])) {
$options = $dbParams['options'];
} else {
$options = array();
}
$adapter->injectProfilingStatementPrototype($options);
return $adapter;
},
),
),
);
Any help is appreciated.
thanks.
It had worked for me. now i am getting Zend/db Queries in developer tool.so Well you can add some code in your composer.json file like
"bjyoungblood/bjy-profiler": "dev-master",
then update composer!
php composer.phar update
then let your application know about your changes.change your application.config
return array(
'modules' => array(
'ZendDeveloperTools',
'DoctrineModule',
'DoctrineORMModule',
'Application',
'BjyProfiler'
),
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor'
),
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php'
)
)
);
hope it will also work for you.
thank you.

Zend Framework 2 + Doctrine 2 Custom configuration with YML mapping not working

I am trying a different configuration to working with a new Project.
What I want to do is to create a database by writing sql on hand.
After it I want to do a convert-mapping from database to "YML" instead of php annotations.
So, to finish it, I want to convert those YML mapping information to Doctrine Entity, inside a ZF2 Module.
I am Using in composer:
"doctrine/doctrine-orm-module" : "0.7.0",
"doctrine/doctrine-module" : "0.7.*",
In global.php configuration
'doctrine' => array(
'connection' => array(
// default connection name
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => '****',
'dbname' => 'gear',
'charset' => 'utf8'
),
)
)
),
On the Target Module
'doctrine' => array(
'driver' => array(
/* This is where you can change the Mapping Driver */
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities_yaml'
),
),
'application_entities_yaml' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
'paths' => array(__DIR__ . '/../src/' .__NAMESPACE__. '/Yml')
),
),
),
I am looking to use a custom place to put the YML annotations, on a ZF2 Action I use a exec thats generate this command:
vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping --namespace="Gear\\Entity\\" --force --from-database yml module/Gear/src/Gear/Yml
Who saves correcty the YML mapping data into the folder module/Gear/src/Gear/Yml
It is exactly the path that I put into "application_entities_yaml" in the module config file.
But when i try to finally create the Entitys to get the work done, with this command:
vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate-entities module/Gear/src/
or with
vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate-entities --generate-annotations=1 module/Gear/src/
I got just:
'No Metadata Classes to process.'
I need to discovery how make Doctrine recognize where i put the metadata classes, to avoid this errors and go on with the project. I'll work with YML entities cuz is the best way to I teach non 'php' programmers write entities by the way. So is important to work with YML.
How to make Doctrine recognize those mapping and convert to entity without problems?
On the Target Module
Just change the Application\Entity to Module\Entity aka Gear\Entity and it works!
'doctrine' => array(
'driver' => array(
/* This is where you can change the Mapping Driver */
'orm_default' => array(
'drivers' => array(
'Gear\Entity' => 'application_entities_yaml'
),
),
'application_entities_yaml' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
'paths' => array(__DIR__ . '/../src/' .__NAMESPACE__. '/Yml')
),
),
),

Categories