Categories


Loading feed
Loading feed
Loading feed

Tutorial : Using Zend Framework Without PDO


This is a very quick tutorial on how to allow developers to develop using the Zend Framework without having to or being able to install PDO on their servers.

I have to warn you though, this is not an official extension of the Zend Framework just yet.

Anyway, let's get rolling as a lot of you are very anxious and happy about that.

About:

This is a merge of PEAR (http://pear.php.net) and the Zend Framework (http://framework.zend.com) in order to give the ability to everyone to be able to connect to as many databases as possible with the Zend_Db database layer.

Quickly, that is a way to connect to may different databases having the same api than the normal Zend_Db without PDO (Pecl) but only PHP.

But..but .. How?!:

Well first of all you need to go to http://dev.agoraproduction.com/zend/ and get the latest Php-x.y.z.tar (At the time of this tutorial (Php-0.1.0.tar)) and get the Db.php file.

Once you are done with this, you will untar the Php-x.y.z.tar and place the Php/ directory into

Zend/Db/Adapter/

(So it looks like Zend/Db/Adapter/Php/ )

After this, you have to replace the current Zend/Db.php with the one I have provided. If you are scared of doing so, it is very understandable and you can refuse to do it, but the drivers won't work. The only thing I added in the Zend_Db class is an elseif to handle the new driver calling (I will show later).

Are you done ? So now you directory structure should look like this:

Zend/
-> Db/
-> Adapter/
-> ...
-> Php/
-> Sqlite.php, Mysql.php, Mssql.php, Oci8.php, Pgsql.php
-> Db.php
-> ...
p. Done ? Good! :-)

Supported Databases:

There are currently 5 different RDBMS supported

  • MySQL (Zend/Db/Adapter/Php/Mysql.php)
  • MsSQL (Zend/Db/Adapter/Php/Mssql.php)
  • PostgreSQL (Zend/Db/Adapter/Php/Pgsql.php)
  • Oracle (Oci8) (Zend/Db/Adapter/Php/Oci8.php)
  • Sqlite (Zend/Db/Adapter/Php/Sqlite.php)

How to call them:

I have tried to make this as easy as possible.. do you remember how to do it usually with PDO ?

<?php
/**
* This is the PDO way. Please notice the PDO_MYSQL
*/
require_once 'Zend.php';
require_once 'Zend/Db.php';

$config = array(
'host' => 'localhost',
'username' => 'sqluser',
'password' => 'sqlpass',
'dbname' => 'databasename',
);

$db = Zend_Db::factory('PDO_MYSQL', $config);
print_r($db->fetchAll("SELECT * FROM tablename"));
print_r($db->fetchRow("SELECT rowname FROM tablename"));
// .. etc.
?>

You noticed the PDO_MYSQL right ? Well the only thing one has to do in order to use the php only drivers is use PHP_MYSQL, PHP_OCI8, PHP_PGSQL, PHP_SQLITE.

Example:

<?php
/**
* This is the PHP (No pdo) way. Please notice the PHP_MYSQL
*/
require_once 'Zend.php';
require_once 'Zend/Db.php';

$config = array(
'host' => 'localhost',
'username' => 'sqluser',
'password' => 'sqlpass',
'dbname' => 'databasename',
);

$db = Zend_Db::factory('PHP_MYSQL', $config); // Here!!!! it happened here!
print_r($db->fetchAll("SELECT * FROM tablename"));
print_r($db->fetchRow("SELECT rowname FROM tablename"));
// .. etc.
?>

So yes, basically it's the same thing you have to do.

Current working functions:

  • fetchAll
  • fetchOne
  • fetchRow
  • fetchCol
  • query
  • update
  • insert
  • describeTable
  • listTables
  • setFetchMode (Has to be adjusted to ZF)
  • quote (MDB2::escape())
  • And other functions are to come later as I am implementing them.

About me:

No one really wants to read that right? Here's my blog: http://blog.agoraproduction.com
David Coallier (davidc@agoraproduction.com)

Enjoy! :)

Comments


Thursday, February 22, 2007
DESCRIBE TABLE PRIMARY KEY PROBLEM
7:18PM PST · david_coallier
Monday, April 30, 2007
IBM DB2
9:19AM PDT · caballero
Saturday, June 16, 2007
SIMPLE MYSQL ZEND ADAPTER (NON PDO ) FOR THE NEW ZEND FRAMEWORK
4:06AM PDT · jahjah
SIMPLE MYSQL ZEND ADAPTER (NON PDO ) FOR THE NEW ZEND FRAMEWORK
4:07AM PDT · jahjah
Friday, July 6, 2007
[RE]: SIMPLE MYSQL ZEND ADAPTER (NON PDO ) FOR THE NEW ZEND FRAMEWORK
1:02PM PDT · david_coallier
Wednesday, August 29, 2007
DOES IT SUPPORT ZF 1.0.1?
8:13AM PDT · aaaa0441
Tuesday, October 16, 2007
ZEND FRAMEWORK FETCHALL FROM MODELS (TABLE CLASSES) FIX
8:44PM PDT · Christof Coetzee [unregistered]
Sunday, January 6, 2008
NEW RELEASE
12:50AM PST · david_coallier
NEW RELEASE TAKE 2
12:52AM PST · david_coallier
Thursday, January 10, 2008
NEW DB.PHP REQUIRED?
9:41AM PST · jaruz
Monday, March 17, 2008
FIX
8:20AM PDT · bacrhr
Thursday, June 19, 2008
ZF 1.5 SUPPORT?
10:56PM PDT · elpadolito