Integrating with codeigniter

383 views
Skip to first unread message

Experimenter

unread,
Feb 11, 2013, 1:00:55 PM2/11/13
to not...@googlegroups.com
NotORM can be integrated with codeigniter in 3 easy steps. 

1. unzip the contents of NotORM in application/libraries folder. You should have NotORM.php and NotORM directory in the libraries folder. The tests folder can be removed.

2. create a new file Orm.php in the application/libraries folder as listed below:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
require_once "NotORM.php" ;

class Orm extends NotORM{
    
    function __construct(NotORM_Structure $structure = null, NotORM_Cache $cache = null){
        $CI =& get_instance();
        $CI->load->database(); //required to get the db parameters 
        //Please set $active_record = FALSE & $db['default']['autoinit'] = FALSE; 
        //in case you don't intend to use them, for low resource usage and better performance
        $connection = new PDO("{$CI->db->dbdriver}:dbname={$CI->db->database};host={$CI->db->hostname}",
  $CI->db->username,
  $CI->db->password);
        parent::__construct($connection, $structure, $cache);
    }
}

3. Its done. You can now use NotORM in your controllers by simply loading the library orm. and accessing the new NotORM object as $this->orm. example given below.

class Welcome extends CI_Controller{
    function index(){
        $this->load->library(orm);
        //NotORM object is now available as $this->orm
        foreach ($this->orm->users() as $user){
              echo $user['name'];
        }
    }
}
------------------------------------
Enjoy. CI with NotORM - CI 

Experimenter

unread,
Feb 18, 2013, 4:02:56 AM2/18/13
to not...@googlegroups.com
In case you are using a db prefix you will need to modify Orm class written in libraries/Orm.php as follows:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
require_once "NotORM.php" ;

class Orm extends NotORM{
    
    function __construct(NotORM_Structure $structure = null, NotORM_Cache $cache = null){
        $CI =& get_instance();
        $CI->load->database(); //required to get the db parameters 
        //Please set $active_record = FALSE & $db['default']['autoinit'] = FALSE; 
        //in case you don't intend to use them, for low resource usage and better performance
        
        //Defining the structure to  use the db prefix declared in database config
        if (!isset($structure) && $CI->db->dbprefix!=''){
            $structure = new NotORM_Structure_Convention(
                $primary = 'id',
                $foreign = '%s_id',
                $table = '%s',
                $prefix = $CI->db->dbprefix
            );
        }
        
        // Defining a new PDO connection using database config values

Experimenter

unread,
Feb 18, 2013, 10:56:24 AM2/18/13
to not...@googlegroups.com
for sqlite support with notorm in codeigniter  Orm class in libraries/Orm.php will need to be written as follows:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
require_once "NotORM.php" ;

class Orm extends NotORM{
    
    function __construct(NotORM_Structure $structure = null, NotORM_Cache $cache = null){
        $CI =& get_instance();
        $CI->load->database(); //required to get the db parameters 
        //Please set $active_record = FALSE & $db['default']['autoinit'] = FALSE; 
        //in case you don't intend to use them, for low resource usage and better performance
        
        //Defining the structure to  use the db prefix declared in database config
        if (!isset($structure) && $CI->db->dbprefix!=''){
            $structure = new NotORM_Structure_Convention(
                $primary = 'id',
                $foreign = '%s_id',
                $table = '%s',
                $prefix = $CI->db->dbprefix
            );
        }
        
        // Defining a new PDO connection using database config values
        if ($CI->db->dbdriver=='sqlite'){
            $connection = new PDO("{$CI->db->dbdriver}:{$CI->db->database}");
        } else {
            $connection = new PDO("{$CI->db->dbdriver}:dbname={$CI->db->database};host={$CI->db->hostname}",
  $CI->db->username, $CI->db->password);
        }
        parent::__construct($connection, $structure, $cache);
    }
}

----------------------------------------------------------------------
Thankyou for reporting the problems and making notorm support better in CI. I will be posting the Orm class on google code soon.

vector9x

unread,
Sep 24, 2013, 10:57:29 AM9/24/13
to not...@googlegroups.com
I just found a better way to do it. In the way you propose, you are using one database connection for NotORM and another database connection for CodeIgniter. For example, if you use CI's  is_unique[table.field] form validation, it will use the own CodeIgniter DB connection, additional to the NotORM's.

So,  you can set CodeIgniter to use pdo driver as follows (database.php):

$db['default']['hostname'] = 'mysql:host=localhost;dbname=thedb';   // <<<
$db['default']['username'] = 'theuser';
$db['default']['password'] = 'thepass';
$db['default']['database'] = 'thedb';
$db['default']['dbdriver'] = 'pdo';   // <<<


Then, in the orm.php file, we can use:

$CI =& get_instance();
$CI->load->database();

$connection = $CI->db->conn_id;  // <<<
parent::__construct($connection, $structure, $cache);
        
We're using the own CI's PDO connection instead of creating a new one.
I noticed the problem of two connections when using a shared webserver that restricted the number of db connections to one.




Instead of creating a new connection, you can configure CodeIgniter to use

Jan Paták

unread,
Jun 20, 2014, 4:52:30 AM6/20/14
to not...@googlegroups.com
Could anyone help me with following?
I am using codeigniter with modules extension (https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc) and would like to use NotORM library.
Each of my modules has own table prefix and it is trouble for me.
I haven't found any working solution how to use prefixes when combining datas from 2 or more modules.
Any suggestion how to make it?

Dne pondělí, 18. února 2013 10:02:56 UTC+1 Experimenter napsal(a):
Reply all
Reply to author
Forward
0 new messages