A Web Developer's Diary

Run Code Igniter on H2 database engine using PDO and Quercus

7/6/2012

4 Comments

 
In my previous post I explained how to setup PHP and H2 database engine using a platform-independent Java stack, a.k.a. JAMP. I managed to get the Code Igniter framework running on JAMP, with some tweaking of the Code Igniter PDO driver. Here are the steps to make it work.
If you would like to skip these steps and jump immediately into some coding action, download the demo.
(Unzip and go into directory jamp-ci-demo/, then type `mvn jetty:run` from the command line. You need Maven installed to execute this command.)
jamp-ci-demo.zip
File Size: 2341 kb
File Type: zip
Download File

First, checkout a copy of JAMP at github (git clone https://github.com/webdevelopersdiary/jamp.git).

Then download Code Igniter and put in the web root (src/main/webapp/).

Because Code Igniter likes pretty urls (ie. /controller_name/method_name) I added support for mod_rewrite to JAMP. It can parse mod_rewrite rules which are located in .htaccess directly in the web root (src/main/webapp/). Add src/main/webbapp/.htaccess with mod_rewrite rules:
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI} ^(system|application).*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Start up the JAMP web server (make sure .htaccess exists before starting the web server or it will have trouble discovering it, then execute mvn jetty:run). Verify Code Igniter is running correctly by going to http://localhost:8080/welcome (the default controller).

If you see a pesky error '$assign_to_config is an undefined variable', go to src/main/webapp/index.php and add
$assign_to_config = array();
under the section 'CUSTOM CONFIG VALUES' (line 110 of index.php) and that should get rid of the error.

Next we configure Code Igniter to connect to the H2 database using PDO. Edit the configuration located at src/main/webapp/application/config/database.php and set these settings:
$db['default']['hostname'] = 'mysql:';
$db['default']['dbdriver'] = 'pdo';
Next, we have to make a slight modification to Code Igniter's PDO driver. Change the method num_rows() from src/main/webapp/system/database/drivers/pdo/pdo_result.php to:

	/**
* Number of rows in the result set
*
* @access public
* @return integer
*/
function num_rows()
{
$sql = $this->result_id->queryString;
if(NULL === $sql) {
$sql = print_r($this->result_id, TRUE);
$quercus_preg = '/\Aresource\(PDOStatement\[(.*)\]\)\z/is';
$php_preg = '/\APDOStatement Object\s*\(.*=>\s*(.*)\)\s*\z/is';
if(preg_match($quercus_preg, $sql)) {
// We're running in Quercus
$sql = preg_replace($quercus_preg, '\1', $sql);
} else if(preg_match($php_preg, $sql)) {
// We're running in PHP
$sql = trim(preg_replace($php_preg, '\1', $sql));
} else {
// No clue about what is running this script!
$sql = NULL;
}
}
if (is_numeric(stripos($sql, 'SELECT')))
{
$count = count($this->result_id->fetchAll());
$this->result_id->execute();
return $count;
}
else
{
return $this->result_id->rowCount();
}
}
Everything should now be in place to connect to the H2 database using Code Igniter's ActiveRecord library. Let's test it! Create a test controller application/controller/test.php:
<?php

class Test extends CI_Controller {
public function index() {
$this->load->database();
$sql = $this->db->query('CREATE TABLE IF NOT EXISTS test (value DATETIME)');
$sql = $this->db->set('value', 'NOW()', FALSE)->insert('test');
var_dump($this->db->get('test')->result());
}
}
Voila! Go to http://localhost:8080/test and enjoy a working Code Igniter on H2 database using ActiveRecord :-)

Note: I used Code Igniter version 2.1.2, Quercus version 4.0.28 and H2 database version 1.3.167.
4 Comments

Minifying, Combining and Caching CSS and JS with Code Ingiter

6/29/2012

0 Comments

 
So you realised all these HTTP requests are taking up a lot of page load time? No worries! You'll be up to speed in no-time.

First download the improved version of the CI plugin Carabiner from github (or download zip), originally written by Tony Dewan and now maintained by Mike Funk. Copy the config and libraries folder into your CI installation by putting them in: application/third_party/carabiner/ and add the package to application/config/autoload.php by adjusting the auto loaded packages:
 $autoload['packages'] = array(APPPATH.'third_party/carabiner'); 
Make sure the application/third_party/carabiner/config/carabiner.php configuration file is set properly (e.g. make sure the cache path exists and is writeable) and you can test your first Minify/Combine/Cache experience using this example controller:
  
class TestCarabiner extends CI_Controller {
public function index() {
$this->load->library('carabiner');
$this->carabiner->css('file1.css');
$this->carabiner->css('file2.css');
$this->carabiner->js('file1.js');
$this->carabiner->js('file2.js');
$this->carabiner->display(); // output html which loads cached files
}
}
Besides using $this->carabiner->display() you can also use $this->carabiner->display_string() to get the HTML as a string instead of outputting it directly to the browser (e.g. so you can use it in a template). You can also call carabiner directly from a Code Igniter view using $this->carabiner->display(). Another useful function call is $this->carabiner->empty_cache('both', 'yesterday') which cleans up old cache files. Full documentation is available at this page.
0 Comments

    Author

    Blog about random challenges of a web developer.

    Archives

    April 2015
    May 2013
    January 2013
    July 2012
    June 2012

    Categories

    All
    Aws
    Cdn
    Code Igniter
    Css
    H2database
    Jamp
    Java
    Javascript
    Magento
    Maven
    Mysql
    Opencl
    Php
    Play-framework
    Quercus
    Scala
    Ubuntu

    RSS Feed

Powered by Create your own unique website with customizable templates.