Zend Framework: MongoDB session saveHandler

31 05 2011

In this third part about the Zend Framework i am diving into the world of Zends implementation for sessions. Sessions are used to provide a way to save data between different page requests.

The Zend Framework has an easy way of managing sessions by creating a new instance of the Zend_Session_Namespace class. This class provides an easy to use API on the Zend_Session managed superglobal $_SESSION. By default the session data is saved to the filesystem which works perfect for simple applications using a single webserver but will be more problematic when multiple webservers are used all with their own filesystem.

A solution for this problem is saving the session data to a central database which is available to all webservers. The Zend Framework provides a class which can be used for this: Zend_Session_SaveHandler_DbTable which can be configured completely as a resource within the application.ini file.

Using the database as a central source for session data gives some other worries though:

  • You have to make sure the database will be available at all time
  • Clustering databases for high availability is something not all persons have knowledge on
  • Managing the session tables within these databases poses some headaches when changes to the existing table are needed.

Another approach is using a memory based store like memcache, but in this post i would like to use the “new” kid in town called MongoDB, a scalable, high-performance, open source, document-oriented database. For this we are going to write a new session save handler for Zend Framework called GcLib_Zend_Session_SaveHandler_MongoDb. The source of this class can be downloaded here.

Read the rest of this entry »


PHP OCI extension for Oracle: Execution Mode

11 05 2010

In the PHP world it is common to use an open source database like MySQL for persisting data. But this post is about a less common used database – Oracle – and especially the execution mode found in the PHP OCI extension for Oracle.

The PHP OCI extension for Oracle provides two execution modes: OCI_DEFAULT and OCI_COMMIT_ON_SUCCESS. The default execution mode is OCI_COMMIT_ON_SUCCESS and this mode will automatically commit any (successful) SQL statement. The OCI_DEFAULT mode is mostly used when multiple statements are executed in one batch and should be committed (or rolled back) as one transaction.

Although most PHP developers (and PHP frameworks like Zend Framework) are using the OCI_COMMIT_ON_SUCCESS execution mode as their default mode, I personally think that you should not use this mode: Using the OCI_COMMIT_ON_SUCCESS mode will auto-commit any successful SQL statement, even when a SELECT statement is executed!

Read the rest of this entry »

Zend Framework: Application resources

27 10 2009

In this second part of my Zend Framework quest, i will dive in the wonderfull world of application resource plugins. Zend_Application provide several standard resource plugins for database access, session management, routing etc. You can find more info about these standard Resource Plugins here.

The fun part of these Resource Plugins is that they can be configurated completely within the application.ini (or application.xml) configuration file and are loaded automatically by the Application Bootstrapper.  For example you can define a new Route resource “myroute” referencing the module/controller/action without writing any code like this:

; Router resources
resources.router.routes.myroute.route = "hello/:myname"
resources.router.routes.myroute.defaults.module = "mymodule"
resources.router.routes.myroute.defaults.controller = "index"
resources.router.routes.myroute.defaults.action = "index"

Which will route the following url “http://localhost/hello/world” to the standard controller action from the “mymodule” module, with the request parameter “myname”  filled with “world”.

While Zend Framework ships with a number of standard resource plugins, the intention is that developers should write their own to encapsulate their own initialization needs. So lets begin with our own custom resource plugin for initializing application logging using Zend_Log.

Read the rest of this entry »

Zend Framework: Project setup

7 10 2009

It has been a while since i last wrote a post about working with the Zend Framework and the, then rather new, package Zend_Amf. The project structure was an attempt to integrate the Zend MVC with Zend Amf (and of course try out some new features in the Flash player 10).
Since that post, Zend has released some major releases with some rather big changes, like the Zend_Application package combined with the Bootstrap class.

Because i am not a real PHP programmer (consider myself as a junior in that area), it is rather difficult to learn all the ins-and-outs of the framework and i rely heavily on what other people write on the internet about the several topics considering the Zend Framework.

So i decided to bundle all my findings into one project setup and publish the sources on Google code. This way i always have access to these sources and i can refer to them within coming blog posts.

Read the rest of this entry »