karim_fci
karim_fci

Reputation: 1242

Enabling BjyProfiler module in zend framework 2

I just installed zend developer tools module and BjyProfiler module in my application. Zend developer tools is working fine and both zend developer tools module and BjyProfiler module showing in module list in toolbar. But BjyProfiler not working properly, when i click database button of zend developers toolbar then it show like following: enabling bjyprofiler

I go through BjyProfiler readme in github. After reading readme, I am not sure where to put this code to enable BjyProfiler for query debugging.

$profiler = $sl->get('Zend\Db\Adapter\Adapter')->getProfiler();
$queryProfiles = $profiler->getQueryProfiles();

In summary, my problem is where to put this code and how to enable BjyProfiler successfully to debug query error. Thanks for your kind attention.

Upvotes: 4

Views: 2007

Answers (1)

Jaime Rodriguez
Jaime Rodriguez

Reputation: 11

Add to aplication.config.php:

'modules' => array(
    //...
    'BjyProfiler',
    'ZendDeveloperTools',
    'MyOtherModule',
    //...
),

Create bjyprofiler.local.php in config/autoload & add:

$dbParams = array(
    'database'  => 'ZF2sample',
    'username'  => 'root',
    'password'  => '123456',
    'hostname'  => 'localhost',
    // buffer_results - only for mysqli buffered queries, skip for others
    'options' => array('buffer_results' => true)
);

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;
            },
        ),
    ),
);

F5 in browser!

Upvotes: 1

Related Questions