Using NuoDB from the PHP Zend Framework

The Zend Framework is an object oriented PHP framework that can be used to construct interactive web applications.  NuoDB supports both Zend Framework version 1 and version 2 .  In this TechBlog article, I will show you how to use NuoDB with both Zend Framework v1 and v2.  I will use the example applications that are provided in the Zend Framework documentation and show you how to make them work with NuoDB.

In this blog post, I am using clean a install of 64-bit Ubuntu desktop 12.04.http://www.ubuntu.com/download/desktop  (note: the commands and paths below may be different on other Linux distributions).

Install Apache2 & PHP

First we need to install Apache2, PHP, and enable the Apache2 rewrite module.  You can do that with the following commands:

$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-php5
$ sudo a2enmod rewrite

Install and Configure NuoDB

Download NuoDB: http://www.nuodb.com/download

Install NuoDB and run the quickstart: http://www.nuodb.com/nuodb-online-documentation/tasks/t_InstallConfigLix_inst.html

PHP Information

Before we install the NuoDB PHP PDO Driver, we need to obtain some information about the installed PHP Distribution.  Here is what I see on my machine:

$ php -i | grep "PHP Version"
PHP Version => 5.3.10-1ubuntu3.6
PHP Version => 5.3.10-1ubuntu3.6
$ php -i | grep "Thread Safety"
Thread Safety => disabled
$ php -i | grep "Configuration File"
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini
$ php -i | grep "extension_dir"
extension_dir => /usr/lib/php5/20090626 => /usr/lib/php5/20090626

Install and Configure NuoDB PHP PDO Driver

To use NuoDB from PHP, you will need to install and configure the NuoDB PHP PDO Driver.  The source code is available on GitHub,  but you probably won’t need to build it yourself because your NuoDB installation already contains pre-built drivers for PHP 5.3 NTS and PHP 5.4 NTS.

Having obtained the PHP Information above, we can copy the correct NuoDB PHP PDO Driver to that extension_dir with the following command:

$ sudo cp /opt/nuodb/drivers/php_pdo/php-5.3.NTS/pdo_nuodb.so /usr/lib/php5/20090626

Next, we need to modify the PHP Configuration to dynamically load the NuoDB PHP PDO Driver.  This is done by adding “extension=pdo_nuodb.so” to your PHP initialization files.  On Ubuntu, PHP is configured to scan specific directories for PHP .ini files.  The command line version of PHP scans the /etc/php/cli/conf.d directory.  When PHP is executed from Apache2, PHP is configured to scan the /etc/php5/apache2/conf.d directory.  To add NuoDB to those PHP configurations, we can use that extension statement in a file named nuodb.ini and copy that file to those config directories:

$ echo "extension=pdo_nuodb.so" > /tmp/nuodb.ini
$ sudo cp /tmp/nuodb.ini /etc/php5/cli/conf.d/
$ sudo cp /tmp/nuodb.ini /etc/php5/apache2/conf.d/
$ sudo /etc/init.d/apache2 restart

Verify Proper Loading of NuoDB PHP PDO Driver

We can verify the proper configuration and loading of the NuoDB PHP PDO Driver by calling the phpinfo() function from a .php file on the default Apache2 website.  Example:

$ echo "<? phpinfo(); ?>" > /tmp/phpinfo.php
$ sudo cp /tmp/phpinfo.php /var/www/

Then open a web browser on the URL:

http://localhost/phpinfo.php

The PDO section should display “nuodb” in “PDO drivers”:

phpinfo

NuoDB Zend Framework Repository

We have a repository on GitHub that holds the files necessary to support NuoDB with the Zend Framework.  You can download it as follows:

$ cd ~/Downloads
$ wget -O nuodb-php-pdo-zend.tar.gz https://github.com/nuodb/nuodb-php-pdo-zend/tarball/master
$ cd ..
$ tar -xzf ~/Downloads/nuodb-php-pdo-zend.tar.gz
$ mv nuodb-nuodb-php-pdo-zend-* nuodb-php-pdo-zend

Zend Framework v1

There is a Zend Framework v1 quickstart example on the framework.zend.com website.  It is located within the Zend Framework 1 documentation.  The quickstart is virtual guestbook application where web visitors can sign the guestbook.

You should start by downloading Zend Framework v1 from the framework.zend.com website and extract it:

$ cd ~/Downloads
$ wget https://packages.zendframework.com/releases/ZendFramework-1.12.3/ZendFramework-1.12.3.tar.gz
$ cd ..
$ tar -xzf ~/Downloads/ZendFramework-1.12.3.tar.gz

The NuoDB Zend Framework repository contains a file Nuodb.php.  That file is the NuoDB Zend Framework v1 Data Adapter.  You must copy Nuodb.php into your Zend Framework v1 library/Zend/Db/Adapter/Pdo directory:

$ cp ~/nuodb-php-pdo-zend/Nuodb.php ~/ZendFramework-1.12.3/library/Zend/Db/Adapter/Pdo/

Now follow the instructions for the quickstart example:

http://framework.zend.com/manual/1.12/en/learning.quickstart.html.

Or you can extract the completed example from the file ~/nuodb-php-pdo-zend/nuodb_zf1_quickstart.tar.gz.

The quickstart example on the framework.zend.com website uses Sqlite.  It is easy to use NuoDB instead of Sqlite.  When it comes time to configure the db-adapter, use the following command to configure NuoDB instead:

$ zf.sh configure db-adapter 'adapter=PDO_NUODB&host=localhost&username=dba&password=goalie&dbname=test@localhost&schema=guestbook&options.autoQuoteIdentifers=false' production

At this point, start your NuoDB Storage Manager/Transaction Engine on the database test@localhost, then use the following nuosql command to create the schema, table, and data:

$ /opt/nuodb/bin/nuosql test@localhost --user dba --password goalie
SQL> DROP SCHEMA guestbook CASCADE IF EXISTS;
SQL> CREATE SCHEMA guestbook;
SQL> USE guestbook;
SQL> CREATE TABLE guestbook (
 "id" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
 "email" VARCHAR(32) NOT NULL DEFAULT 'noemail@test.com',
 "comment" TEXT NULL,
 "created" DATETIME NOT NULL
 );
SQL> INSERT INTO guestbook ("email", "comment", "created") VALUES
 ('ralph.schindler@zend.com',
 'Hello! Hope you enjoy this sample zf application!',
 NOW());
SQL> INSERT INTO guestbook ("email", "comment", "created") VALUES
 ('foo@bar.com',
 'Baz baz baz, baz baz Baz baz baz - baz baz baz.',
 NOW());

That’s all that is needed.

Running the Zend Framework v1 quickstart

Open web browser to: http://zf1quickstart/guestbook

ZF1_quickstart_1

 

Sign the guestbook:

ZF1_quickstart_2

 

 

Zend Framework v2

There is a Zend Framework v2 tutorial example on the framework.zend.com website.  It is located within the Zend Framework v2 documentation. The tutorial is music album inventory application.

You should start by downloading Zend Framework v2 from the framework.zend.com website and extract it:

$ cd ~/Downloads
$ wget https://packages.zendframework.com/releases/ZendFramework-2.2.1/ZendFramework-2.2.1.tgz
$ cd ..
$ tar -xzf ~/Downloads/ZendFramework-2.2.1.tgz

Now follow the instructions for the ZF2 tutorial example:

http://framework.zend.com/manual/2.2/en/user-guide/skeleton-application.html. 

Or you can extract the completed example from the file ~/nuodb-php-pdo-zend/nuodb_zf2_tutorial.tar.gz.

In the “Database and modules” section, instead of using MySQL, create the following schema, table, and data in NuoDB:

$ /opt/nuodb/bin/nuosql test@localhost --user dba --password goalie
SQL> DROP SCHEMA zf2tutorial CASCADE IF EXISTS;
SQL> CREATE SCHEMA zf2tutorial;
SQL> USE zf2tutorial;
SQL> CREATE TABLE "album" (
 "id" int NOT NULL generated by default as identity,
 "artist" varchar(100) NOT NULL,
 "title" varchar(100) NOT NULL,
 PRIMARY KEY (id)
 );
SQL> INSERT INTO "album" ("artist", "title")
 VALUES  ('The  Military  Wives',  'In  My  Dreams');
SQL> INSERT INTO "album" ("artist", "title")
 VALUES  ('Adele',  '21');
SQL> INSERT INTO "album" ("artist", "title")
 VALUES  ('Bruce  Springsteen',  'Wrecking Ball (Deluxe)');
SQL> INSERT INTO "album" ("artist", "title")
 VALUES  ('Lana  Del  Rey',  'Born  To  Die');
SQL> INSERT INTO "album" ("artist", "title")
 VALUES  ('Gotye',  'Making  Mirrors');

Database Configuration

Modify config/autoload/global.php (in the Zend Skeleton root, not inside the Album module) with following code:

<?php
 return array(
    'db' => array(
    'driver'         => 'Pdo',
    'dsn'            => 'nuodb:database=test@localhost;schema=zf2tutorial',
    ),
    'service_manager' => array(
       'factories' => array(
           'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
 );

You should put your database credentials in config/autoload/local.php so that they are not in the git repository (as local.php is ignored):

<?php
  return array(
     'db' => array(
         'username' => 'dba',
         'password' => 'goalie',
      ),
   );

Running the Zend Framework v2 tutorial

Open web browser to: http://zf2-tutorial/album

zf2_tutorial_1

Add new album

Tags: 

Vivek
Anonymous's picture
HI,

HI,
i am unable to find the pdo_nuodb driver in my phpinfo().

I am using ubuntu i done the above steps correctly

i can see the nuodb.ini file in cli folder.

Hello Vivek,

Hello Vivek,

What version of ubuntu are you running?

Can you see the nuodb.ini file in the /etc/php5/apache2/conf.d directory?

Can you double check that nuodb.ini contains:

extension=pdo_nuodb.so

-tom

exfromtheleft
Anonymous's picture
<p>from what i&#39;ve just

from what i've just read when using the zend framework 1 or 2, queries made with ZendDB do not change one bit as long as the proper drivers and adapters are in place?

cany
Anonymous's picture
<p>sir,</p><p>&nbsp;</p><p>am

sir,
am unable to add new album, edit delete only the listing works fine. i have followed the tutorial http://framework.zend.com/manual/2.2/en/user-guide/modules.html strictly. Dont know where is the missing part, kindly help.
thanks

Desmond
Anonymous's picture
<p>Please can you help me. I

<p>Please can you help me. I downloaded this APP NuoDB, and need assistance on how to open a whatsapp messenger DB file. I have the db file stored on my sd memory card.</p><p>You can email me on: joyouscommunications01@gmail.com</p>

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.