You are on page 1of 4

CI + Doctrine Notes Last Modified: 10-Oct-2010 11:38 AM

Codeigniter 1.7.2 Installation Notes

1. Download the Codeigniter installation zip file and extract under /wamp/www/ciproject
2. To test, browse to http://localhost/ciproject - should display a welcome page
3. To test further:
a. Create this controller Hello.php under ciproject/system/application/controllers

<?php
class Hello extends Controller
{
function Hello() //constructor function
{
parent::Controller();
}
function index() // default action function
{
echo("this is the Hello controller's default action");
}
function world() // another action function
{
echo("hello world by codeigniter");
}
}

b. Browse to http://localhost/ciproject/index.php/hello to see the first message


c. Browse to http://localhost/ciproject/index.php/hello/world to see the second message

4. To remove index.php from URL


a. Create this .htaccess file right under the ciproject directory

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ciproject/index.php/$1 [L]

b. Edit these 2 lines in ciproject/system/application/config/config.php:

$config['base_url'] = "http://localhost/ciproject";

$config['index_page'] = "";

c. Test again by browsing to http://localhost/ciproject/hello and


http://localhost/ciproject/hello/world

©ITworks Pg 1/4
CI + Doctrine Notes Last Modified: 10-Oct-2010 11:38 AM

5. To configure the database connection:


a. Edit the following lines in ciproject/system/application/config/database.php:

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "myusername";
$db['default']['password'] = "mypassword";
$db['default']['database'] = "mydbname";

b. Edit the following line in ciproject/system/application/config/autoload.php:

$autoload['libraries'] = array('database');

c. To test the database connection:


i. Using phpMyAdmin or a similar database utility, create a test table in your
database, with a string test field or two, and add a couple of test records to it.

CREATE TABLE `test` (


`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`testfld1` varchar(255) DEFAULT NULL,
`testfld2` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1

INSERT INTO `test`(`testfld1`) VALUES ('Joe Bloggs');


INSERT INTO `test`(`testfld1`) VALUES ('Jane Doe');

ii. Add this function to the Hello controller:

function testdb()
{
$sql = "SELECT testfld1 FROM test";
$res = mysql_query($sql);
while( $row=mysql_fetch_array($res) )
{
echo($row['testfld1']."<br/>");
}
}

iii. Browse to http://localhost/ciproject/hello/testdb to see your test records.

©ITworks Pg 2/4
CI + Doctrine Notes Last Modified: 10-Oct-2010 11:38 AM

Using Doctrine 1.2.3 with Codeigniter 1.7.2

1. Create the following folder structure: ciproject/system/application/plugins/doctrine/lib

2. Download the Doctrine installation zip file and extract the Doctrine folder and Doctrine.php to
ciproject/system/application/plugins/doctrine/lib

3. Under ciproject/system/application/plugins/ create a new script file doctrine_pi.php as follows:

<?php
// load Doctrine library
require_once APPPATH.'/plugins/doctrine/lib/Doctrine.php';

// load CI database configuration


require_once APPPATH.'/config/database.php';

// allow Doctrine to load Model classes automatically


spl_autoload_register(array('Doctrine', 'autoload'));

// load our database connections into Doctrine_Manager


// loop allows us to use multiple connections later on
foreach ($db as $connection_name => $db_values)
{
// first we must convert to dsn format
$dsn = $db[$connection_name]['dbdriver'] .
'://' . $db[$connection_name]['username'] .
':' . $db[$connection_name]['password'].
'@' . $db[$connection_name]['hostname'] .
'/' . $db[$connection_name]['database'];
Doctrine_Manager::connection($dsn,$connection_name);
}

// CI Model class & our application model classes


require_once BASEPATH.'/libraries/Model.php';
Doctrine::loadModels(APPPATH.'/models');

// (OPTIONAL) CONFIGURATION BELOW


// allow use of "mutators"
Doctrine_Manager::getInstance()->setAttribute(
Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
// default table columns settings notnull and unsigned (for ints)
Doctrine_Manager::getInstance()->setAttribute(
Doctrine::ATTR_DEFAULT_COLUMN_OPTIONS,
array('notnull' => true, 'unsigned' => true));
// default primary key to be named 'id', integer, 4 bytes
Doctrine_Manager::getInstance()->setAttribute(
Doctrine::ATTR_DEFAULT_IDENTIFIER_OPTIONS,
array('name' => 'id', 'type' => 'integer', 'length' => 4));

©ITworks Pg 3/4
CI + Doctrine Notes Last Modified: 10-Oct-2010 11:38 AM

4. Edit the following line in ciproject/system/application/config/autoload.php:

$autoload['plugin'] = array('doctrine');

5. Edit the following line in


ciproject/system/application/plugins/doctrine/lib/Doctrine/Import/Builder.php:

protected $_baseClassesDirectory = ''; //was 'generated'

protected $_generateBaseClasses = false; //was true

6. To test Doctrine set-up:


a. Browse to http://localhost/ciproject/hello etc. to ensure the previous actions still work.
b. Add this function to the Hello controller:

function genmodels()
{
echo('<form action="" method="POST">'.
'<input type="submit" name="action" value="Generate Model Classes
From Database"><br /><br />');
if ($this->input->post('action'))
{
Doctrine::generateModelsFromDb(APPPATH.'/models');
echo("Done!");
}
}

c. Browse to http://localhost/ciproject/hello/genmodels to execute this action.


d. Check the folder ciproject/system/application/models/ - there should PHP files for the
tables in the database, including the test table created earlier.
e. Add this action function to the Hello controller:

function addtestrecs()
{
$t = new Test;
$t->testfld1 = 'John Smith';
$t->save();
$t2 = new Test;
$t2->testfld1 = 'Jack Spratt';
$t2->save();
echo("added 2 test records");
}

f. Browse to http://localhost/ciproject/hello/addtestrecs to execute this action


g. Browse to http://localhost/ciproject/hello/testdb to check that the two new records
have indeed been added.

©ITworks Pg 4/4