Skip to main content

Using CakePHP Migrations as a Standalone Library

·355 words·2 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones

Cakephp version 3 have a nice ORM. When using the cakephp/orm, it may be nice to integrate cakephp/migration than any other migration libraries, even though it uses phinx under the hood.

Lets see how we can install and integrate cakephp/migration in our application.

1
composer require cakephp/migrations:dev-master

The dev-master is currently passed for we need the latest version of master branch. Before this pull request, it was having dependency on cakephp/cakephp, which is not needed.

Our migration console script

 1
 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
#!/usr/bin/env php
<?php
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Datasource\ConnectionManager;
use Migrations\MigrationsDispatcher;

$projectDirectory = dirname(__DIR__);
$file   = $projectDirectory . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$loader = null;

if (file_exists($file)) {

    $loader = require $file;

    if (!defined('CACHE')) {
        define('CACHE', $projectDirectory . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR);
    }
}

if ( ! $loader) {
    throw new RuntimeException('vendor/autoload.php could not be found. Did you run `composer install`?');
}

if (!defined('PHINX_VERSION')) {
    define('PHINX_VERSION', (0 === strpos('@PHINX_VERSION@', '@PHINX_VERSION')) ? '0.6.6' : '@PHINX_VERSION@');
}

try {
    Configure::config('default', new PhpConfig($projectDirectory . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR) );
    Configure::load('app', 'default', false);
} catch (\Exception $e) {
    exit($e->getMessage() . "\n");
}

Cache::setConfig(Configure::consume('Cache'));
ConnectionManager::setConfig(Configure::consume('Datasources'));
$application = new MigrationsDispatcher(PHINX_VERSION);
$application->run();

You may notice the constant CACHE. We can get rid of constants if needed. Normally I copy portions of config/app.default.php and keep in /config/app.php file which uses some of these constants.

Once done, we can run the migration script as

1
bin/cake-phinx

You may probably want to allow necessary permission for the script to execute. You can do via chmod +x bin/cake-phinx

There is another PR 312 waiting for approval. If it is merged you will get all the functionalities of https://book.cakephp.org/3.0/en/console-and-shells/orm-cache.html

PS : If you came across some errors regarding migration plugin not loaded etc. You may want to add the below code before calling MigrationsDispatcher

1
2
3
4
<?php
Cake\Core\Plugin::load('Migrations', [
    'path' => $projectDirectory . '/vendor/cakephp/migrations/',
]);

Related

CakePHP ORM and Logging Queries

·196 words·1 min
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.

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.

Building Modular Applications With PSR-7

·1152 words·6 mins
PSR-7, the HTTP message interfaces opened a new door of creating modular applications. Sadly many of the PSR-7 implementations added many helper methods. So if someone is creating a library that needs a PSR-7 implementations they tie the particular library with the PSR-7 implementation and use these convinient helper methods. So was PSR-15: interfaces for HTTP Middleware and PSR-17: interfaces for HTTP Factories was proposed. When creating a module one of the most challenging part is how to serve the javascript, css and images. We are going to use hkt/psr7-asset which is a fork of Aura.Asset_Bundle . What you want to do is only map the path to the assets folder.