Kohana Tutorials – ORM

Posted by chaabant on Dec 9, 2011

I will be starting new tutorials on Kohana , btw I’m new to Kohana so those posts are more like my learning path using Kohana .

Let start …

KOHANA is a PHP framework , I will make a post later discussing why i choose Kohana and not Yii or Zend of CodeIgniter or phpCake etc…

ORM Stand for : Object-relational mapping

First you need to activate the ORM in KOHANA :

uncomment the ORM module inside the bootstarp.php file available inside the application folder .

1
2
3
4
5
6
7
8
9
10
11
12
13
// in my case I'm using all modules so i uncommented all the modules .
Kohana::modules(array(
     'auth'        => MODPATH.'auth',       // Basic authentication
     'cache'       => MODPATH.'cache',      // Caching with multiple backends
     'codebench'   => MODPATH.'codebench',  // Benchmarking tool
     'database'    => MODPATH.'database',   // Database access
     'image'       => MODPATH.'image',      // Image manipulation
     'orm'         => MODPATH.'orm',        // Object Relationship Mapping
     'unittest'    => MODPATH.'unittest',   // Unit testing
     'userguide'   => MODPATH.'userguide',  // User guide and API documentation
     'tableau'     => MODPATH.'tableau', // Generating html tables .. addon added not included in kohana
   
    ));
// in my case I'm using all modules so i uncommented all the modules .
Kohana::modules(array(
	 'auth'        => MODPATH.'auth',       // Basic authentication
	 'cache'       => MODPATH.'cache',      // Caching with multiple backends
	 'codebench'   => MODPATH.'codebench',  // Benchmarking tool
	 'database'    => MODPATH.'database',   // Database access
	 'image'       => MODPATH.'image',      // Image manipulation
	 'orm'         => MODPATH.'orm',        // Object Relationship Mapping
	 'unittest'    => MODPATH.'unittest',   // Unit testing
	 'userguide'   => MODPATH.'userguide',  // User guide and API documentation
     'tableau'     => MODPATH.'tableau', // Generating html tables .. addon added not included in kohana

	));

The second step is to configure your database connection in Kohana go to (/modules/database/config/database.php)

Edit the file to add your database connection (hostname , database , user and password)

The third step is to add the class declaration inside your model (application/classes/model/)

In our example we will add a new class called : user , the filename should be user.php

for now it will only be a declaration :

1
2
3
4
5
6
<?php defined('SYSPATH') or die('No direct access allowed.');
  class Model_User extends ORM {
 
  // for this basic example we will keep this class empty 
}  
?>
<?php defined('SYSPATH') or die('No direct access allowed.');
  class Model_User extends ORM {

  // for this basic example we will keep this class empty
}
?>

As you can see it should always start with Model then underscore then first letter is capital , the name of the table should be the same as the class that we declared (in my case i have a table called “user“)

Finally in our controller we could do the following requests :

1
2
3
4
5
6
7
8
9
$users = ORM::factory('user');
 
// will return the number of users in my table
$users->count_all()
 
// add a new user to the table
$users->firstName = "John";
$users->lastName  = "Doe" ;
$users->save();
$users = ORM::factory('user');

// will return the number of users in my table
$users->count_all()

// add a new user to the table
$users->firstName = "John";
$users->lastName  = "Doe" ;
$users->save();