Kohana configuration
Posted by chaabant on Dec 9, 2011
When doing a PHP project you will need some kind of configuration since we are not using a CMS like WordPress or Joomla , In Kohana we have to do it manually …
Kohana make this process easy , here’s the steps for having a configuration file and using it in our application .
First Step :
Add a new file (site.php) inside the following directory (application -> config -> site.php)
past the following code inside that file :
1 2 3 4 5 |
<?php defined('SYSPATH') or die('No direct script access.'); return array( 'name' => 'PHP Tutorials', 'tag_line' => "Let's talk about kohana!" ); |
<?php defined('SYSPATH') or die('No direct script access.');
return array(
'name' => 'PHP Tutorials',
'tag_line' => "Let's talk about kohana!"
);
Now to use that configuration we could use the following requests :
1 2 3 4 |
// full array $site_config_1 = Kohana::$config->load('site'); // items inside the array $site_config_2 = Kohana::$config->load('site.name'); |
// full array
$site_config_1 = Kohana::$config->load('site');
// items inside the array
$site_config_2 = Kohana::$config->load('site.name');
$site_config_1 would return the full array that we have in our configuration
while
$site_config_2 would only return PHP Tutorials
We could even go farther and add an array inside the array in the configuration file in that case if we wanted to access a specific child we would call it using site.name.child …
Hope this post helped you , if you have any question let me know .
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();