Setup SQLite in Zend Framework

Like many things in Zend Framework this is very simple. I will here show how I usually do.

First we set the required parameters in application.ini:

[production]
...
;Setup database (SQLITE)
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../db/basic.db"
...

This tells Zend that we will be using a SQLite database (PDO_SQLITE) and then also the path to the SQLite db file (basic.db)

For easy access we will now add the database handle to the Zend registry:
Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
 ...
    protected function _initDbAdapter() {
        $this->bootstrap('db');
        $db = $this->getResource('db');
        Zend_Registry::set('db' , $db);
    }
 ...
}

Then whenever we need to use the database we collect if from the Registry

// Get SQLite db handle from the registry
$db = Zend_Registry::get('db');

// Use the database
$resultset = $db->fetchAll("SELECT id, name FROM projects");

Done!

Tested on Mac OSX 10.6.8 and Zend Framework 1.10.8

Comments are closed.

Niklas Tech Blog
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.