Skip to main content

CakePHP ORM and Logging Queries

·196 words·1 min
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones
Table of Contents

Working with cakephp/orm library, I needed to log all the queries. Cakephp provides a way to do it via cakephp/log.

1
2
3
4
5
6
7
8
9
<?php
use Cake\Log\Log;

Log::config('queries', [
	'className' => 'File',
	'path' => '/my/log/path/',
	'file' => 'app',
	'scopes' => ['queriesLog']
]);

But you are not limited, if you need to configure it to a PSR-3 logger like monolog/monolog

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
use Cake\Log\Log;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

Log::config('default', function () {
    $log = new Logger('cli');
    $log->pushHandler(new StreamHandler('php://stdout'));
    return $log;
});

That was pretty simple and it logs to cli.

Thank you José Lorenzo Rodríguez for providing the necessary information.

How about logging the queries to a debugbar?

Install fabfuel/prophiler

1
composer require fabfuel/prophiler

Configuring debugbar
#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
use Cake\Log\Log;
use Fabfuel\Prophiler\Profiler;
use Fabfuel\Prophiler\Toolbar;
use Fabfuel\Prophiler\DataCollector\Request;
use Fabfuel\Prophiler\Adapter\Psr\Log\Logger;

$profiler = new Profiler();
$toolbar = new Toolbar($profiler);
// add your data collectors
// $toolbar->addDataCollector(new Request());

Log::config('db', function () use ($profiler) {
	$log = new Logger($profiler);
	return $log;
});

Using a PSR-7 framework like zend-expressive, bitExpert/prophiler-psr7-middleware is your friend.

Related

CakePHP ORM and Illuminate Pagination

·304 words·2 mins
Do you know CakePHP version 3 has a lovely ORM which can be used as standalone? Thank you José Lorenzo Rodríguez and every contributor, for your hard work. 1 composer require cakephp/orm That’s it. Working on I noticed I need to do some pagination. Oh, remember we have illuminate/pagination. Why not use it? Problem, there seems no one have implemented it. How could we achieve it? Lets do it. 1 composer require illuminate/pagination If you are using a psr-7 request / response here is the middleware for you.

Eloquent and Pagination Inside Zend Expressive

·154 words·1 min
Recently working with eloquent (Laravel’s orm), zend expressive and zend view, I wanted to integrate pagination. It was simple as registering a Paginator middleware. 1 2 3 4 5 6 7 8 9 10 11 12 <?php use Illuminate\Pagination\Paginator; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; IlluminatePaginator::currentPageResolver(function ($pageName) use ($request) { $params = $request->getQueryParams(); return empty($params[$pageName]) ? 1 : $params[$pageName]; }); IlluminatePaginator::currentPathResolver(function () use ($request) { return $request->getUri()->getPath(); }); and you can call paginate on the Model. Eg : Consider you have a Post model.

Integrating Zend Form in Zend Expressive and Zend View

Example is based using Aura.Di. But the functionality will be same for any containers. First register the service Zend\View\HelperPluginManager, so that we can access the same object. To register the form helpers, create the object of Zend\Form\View\HelperConfig and pass the Zend\View\HelperPluginManager service. Example code with Aura.Di version 3 configuration. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php use Aura\Di\ContainerConfig; use Aura\Di\Container; use Zend\Form\View\HelperConfig; class ViewHelper extends ContainerConfig { public function define(Container $di) { $di->set('Zend\View\HelperPluginManager', $di->lazyNew('Zend\View\HelperPluginManager')); } public function modify(Container $di) { $serviceManager = $di->get('Zend\View\HelperPluginManager'); $helper = new HelperConfig(); $helper->configureServiceManager($servicemanager); } } Creating your own zend-view helper # Create your helper class 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php namespace App\View\Helper; use Zend\View\Helper\AbstractHelper; class HasError extends AbstractHelper { // add as many parameters you want to pass from the view public function __invoke() { // some code } } Registering your helper class. First get the Zend\View\HelperPluginManager service.