CakePHP 3 Tutorial 13: Oracle Database

Submitted by naidim on Fri, 09/30/2016 - 05:03

CakePHP 3 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.

1: Install Requirements

Some requirements you should already have in order to run CakePHP (e.g. PHP 5.5.9+) but you'll also need the Oracle Instant Client and OCI8 driver. PDO_OCI is still in development and is unfinished at the time I'm writing this so we'll not be working with that.

Oracle Instantclient

The Oracle Instant Client is available here: http://www.oracle.com/technetwork/database/features/instant-client/inde… 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.

2: Install Your Application

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

3: Install the CakePHP Oracle Driver

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

Exit config/bootstrap.php and all the following line to load the Plugin:

Plugin::load('CakeDC/OracleDriver', ['bootstrap' => true]);

4: Set Your Database Configuration

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

5: 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');
        ...
    }
}

Next: Optional Requirements

Add new comment