You are browsing a version that has not yet been released. |
Laminas Developer Tools in DoctrineORMModule
If you ever tried Laminas Developer Tools you will surely understand the importance of being able to track performance pitfalls or excessive amount of queries in your applications when developing.
Setup
To setup Laminas Developer Tools, run
$ composer require laminas/laminas-developer-tools
Then enable Laminas\DeveloperTools
in your modules and enable profiling
and the toolbar (see docs of Laminas Developer Tools for that).
Once Laminas\DeveloperTools
is enabled, having
doctrine.entity_manager.orm_default
as your default
EntityManager
, you will notice that the queries performed by the ORM
get logged and displayed in the toolbar.
Customization
If you want to customize this behavior (or track multiple
EntityManager
instances) you can do it in different ways. Please
note that if you have set an SQLLogger
in your configuration, this
functionality won't override it, so you can use these features in total
safety.
Multiple EntityManager/Connection instances and logging
WARNING! These are advanced features! Even if the code is fully tested, this is usually not required for most users!
To setup logging for an additional DBAL Connection or EntityManager, put something like following in your module:
1 <?php
namespace MyNamespace;
class Module
{
public function getConfig()
{
return [
'doctrine' => [
'sql_logger_collector' => [
'other_orm' => [
// name of the sql logger collector (used by Laminas\DeveloperTools)
'name' => 'other_orm',
// name of the configuration service at which to attach the logger
'configuration' => 'doctrine.configuration.other_orm',
// uncomment following if you want to use a particular SQL logger instead of relying on
// the attached one
//'sql_logger' => 'service_name_of_my_dbal_sql_logger',
],
],
],
'laminas-developer-tools' => [
// registering the profiler with Laminas\DeveloperTools
'profiler' => [
'collectors' => [
// reference to the service we have defined
'other_orm' => 'doctrine.sql_logger_collector.other_orm',
],
],
// registering a new toolbar item with Laminas\DeveloperTools (name must be the same of the collector name)
'toolbar' => [
'entries' => [
// this is actually a name of a view script to use - you can use your custom one
'other_orm' => 'laminas-developer-tools/toolbar/doctrine-orm',
],
],
],
];
}
public function getServiceConfiguration()
{
return [
'factories' => [
// defining a service (any name is valid as long as you use it consistently across this example)
'doctrine.sql_logger_collector.other_orm' => new \DoctrineORMModule\Service\SQLLoggerCollectorFactory('other_orm'),
],
];
}
public function onBootstrap(\Laminas\EventManager\EventInterface $e)
{
$config = $e->getTarget()->getServiceManager()->get('Config');
if (isset($config['laminas-developer-tools']['profiler']['enabled'])
&& $config['laminas-developer-tools']['profiler']['enabled']
) {
// when Laminas\DeveloperTools is enabled, initialize the sql collector
$app->getServiceManager()->get('doctrine.sql_logger_collector.other_orm');
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
This example will simply generate a new icon in the toolbar, with the
log results of your other_orm
connection: