This post shows the changes to the ZF2 tutorial application to allow it to run with Oracle Database 11gR2.
Oracle Database SQL identifiers are case insensitive by default so "select Abc from Xyz" is the same as "select abc from xyz". However the identifier metadata returned to programs like PHP is standardized to uppercase by default. After executing either query PHP knows that column "ABC" was selected from table "XYZ".
In PHP code, array indices and object attributes need to match the schema identifier case that is returned by the database. This is either done by using uppercase indices and attributes in the PHP code, or by forcing the SQL schema to case-sensitively use lower-case names.
The former approach is more common, and is shown here.
The instructions for creating the sample ZF2 application are here. Follow those steps as written, making the substitutions shown below.
Schema
In Oracle 11gR2, the schema can be created like:
DROP USER ZF2 CASCADE;
CREATE USER ZF2 IDENTIFIED BY WELCOME
DEFAULT TABLESPACE USERS QUOTA UNLIMITED ON USERS
TEMPORARY TABLESPACE TEMP;
GRANT CREATE SESSION
, CREATE TABLE
, CREATE PROCEDURE
, CREATE SEQUENCE
, CREATE TRIGGER
, CREATE VIEW
, CREATE SYNONYM
, ALTER SESSION
TO ZF2;
CONNECT ZF2/WELCOME
CREATE TABLE ALBUM (
ID NUMBER NOT NULL,
ARTIST VARCHAR2(100) NOT NULL,
TITLE VARCHAR2(100) NOT NULL,
PRIMARY KEY (ID)
);
CREATE SEQUENCE ALBUMSEQ;
CREATE TRIGGER ALBUMTRIGGER BEFORE INSERT ON ALBUM FOR EACH ROW
BEGIN
:NEW.ID := ALBUMSEQ.NEXTVAL;
END;
/
INSERT INTO ALBUM (ARTIST, TITLE)
VALUES ('The Military Wives', 'In My Dreams');
INSERT INTO ALBUM (ARTIST, TITLE)
VALUES ('Adele', '21');
INSERT INTO ALBUM (ARTIST, TITLE)
VALUES ('Bruce Springsteen', 'Wrecking Ball (Deluxe)');
INSERT INTO ALBUM (ARTIST, TITLE)
VALUES ('Lana Del Rey', 'Born To Die');
INSERT INTO ALBUM (ARTIST, TITLE)
VALUES ('Gotye', 'Making Mirrors');
COMMIT;
Driver and Credentials
The driver and credentials are Oracle-specific. Always use the OCI8 adapter in ZF, since it is more stable and has better scalability. Specifying a character set will make connection faster.
zf2-tutorial/config/autoload/global.php:
return array(
'db' => array(
- 'driver' => 'Pdo',
- 'dsn' => 'mysql:dbname=zf2tutorial;host=localhost',
- 'driver_options' => array(
- PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
- ),
+ 'driver' => 'OCI8',
+ 'connection_string' => 'localhost/orcl',
+ 'character_set' => 'AL32UTF8',
),
'service_manager' => array(
'factories' => array(
zf2-tutorial/config/autoload/local.php:
return array(
'db' => array(
- 'username' => 'YOUR USERNAME HERE',
- 'password' => 'YOUR USERNAME HERE',
+ 'username' => 'ZF2',
+ 'password' => 'WELCOME',
),
// Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
Attribute & Index Changes
The rest of the application changes are just to handle the case of the Oracle identifiers correctly.
zf2-tutorial/module/Album/Module.php
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
- return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
+ return new TableGateway('ALBUM', $dbAdapter, null, $resultSetPrototype);
},
),
);
zf2-tutorial/module/Album/view/album/album/add.phtml
$form->prepare();
echo $this->form()->openTag($form);
-echo $this->formHidden($form->get('id'));
-echo $this->formRow($form->get('title'));
-echo $this->formRow($form->get('artist'));
+echo $this->formHidden($form->get('ID'));
+echo $this->formRow($form->get('TITLE'));
+echo $this->formRow($form->get('ARTIST'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
zf2-tutorial/module/Album/view/album/album/delete.phtml
<h1><?php echo $this->escapeHtml($title); ?></h1>
<p>Are you sure that you want to delete
-'<?php echo $this->escapeHtml($album->title); ?>' by
-'<?php echo $this->escapeHtml($album->artist); ?>'?
+'<?php echo $this->escapeHtml($album->TITLE); ?>' by
+'<?php echo $this->esc
Truncated by Planet PHP, read more at the original (another 7231 bytes)