Oracle Database

NOTE: I no longer have access to an Oracle database, however, from what I can tell it all works the same with CakePHP 5 as it did with CakePHP 4.

CakePHP 5 does not provide core support for Oracle databases. An Oracle database driver plugin is available from CakeDC here: https://github.com/CakeDC/cakephp-oracle-driver

It's almost as easy as following the directions there, but there are a few "gotchas" with Oracle.

Install Requirements

Some requirements you should already have in order to run CakePHP (e.g. PHP 8.1+) but you'll also need the Oracle Instant Client and OCI8 driver.

Oracle Instantclient

The Oracle Instant Client is available here: https://www.oracle.com/database/technologies/instant-client/downloads.html Identify, download, and install the appropriate version of the basic and devel packages. I'd recommend sqlplus too just in case things don't work out exactly as they should and you want to do some troubleshooting.

After installation, update your /etc/bashrc file to include the Oracle paths.

export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export PATH=$PATH:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib

Once installed you can test your installation with sqlplus.

# sqlplus [user]/[password]@[server-name-or-ip]:[port]/[database]
SQL*Plus: Release 12.1.0.2.0 Production on Fri Sep 30 05:21:08 2016
Copyright (c) 1982, 2014, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
SQL>quit

OCI8

Installation of course depends on your platform. For RedHat EL 6/7 it's as simple as:

$ pecl install oci8
$ echo extension=oci8.so > /etc/php.d/oci8.ini
$ service httpd graceful

Note: If you run into any trouble installing, install systemtap-sdt-devel and retry:

$ yum install systemtap-sdt-devel
$ export PHP_DTRACE=yes

Note: If you get the error "Fatal error: Call to undefined function oci_connect()" and phpinfo() shows oci8.ini loading but not oci8, check your SELinux settings.

Install Your Application

$ composer create-project --prefer-dist cakephp/app [application-name]

Install the CakePHP Oracle Driver

$ cd [application-name]
$ composer require cakedc/cakephp-oracle-driver

Edit /src/Application.php and add the following to the bootstrap() function to load the Plugin:

 public function bootstrap()
 {
   parent::bootstrap();
   $this->addPlugin(\CakeDC\OracleDriver\Plugin::class, ['bootstrap' => true]);
   ...
 }

Set Your Database Configuration

As usual, edit your /config/app_local.php file and configure your database so the application can communicate with it.

Gotchas

Whether running cake bake or building your model manually, you may run into a few issues. To mitigate those, try the following:

  1. Use all lowercase table names.
  2. Use actual table names, not the names in the bake list (e.g. users_tbl vs. USERSTBL)
  3. In the initialize() function of your Model Table class file (e.g. /src/Model/Table/UsersTable.php) add an alias to to shorten the default alias due to the 30 character limits.
class UsersTable extends Table
{
 public function initialize(array $config)
 {
   ...
   $this->alias('U');
   ...
 }
}