Welcome Guest, Not a member yet? Register   Sign In
ORM integration in CI questions
#1

[eluser]Unknown[/eluser]
What i use currently is something like this..

Tables / fields
* etc_users / id, group_id, name, password
* etc_groups / id, name

configuration
Code:
// database connection(s) setup
$db = new rdp_db__mysql;
$db->set_auth('localhost','user','pass','database');

// the factory / singleton
$kreator=new rdp_crud_creator__mysql;
$kreator->set_db($db);

// other db config
$kreator->set_table_prefix('etc_');

// table lists (also has an auto-discovery mode)
$kreator->add_words('user','users');
$kreator->add_words('group','groups');

The obj_user class
Code:
class obj_user extends rdp_crud {
    
    function set_name($text)
    {
        $this->data['name']=$text;
    }

    function set_password($text)
    {
        $this->data['password']=md5($text);
    }
    
    // on object load
    function __init()
    {
        if($this->data['username']=='')
        {
            $this->data['username']='TEMPUSER';
            $this->save();
        }
    }
    
    // on delete - true if can be deleted, false if not
    function __delete()
    {
        if($this->data['name']=='root')
        {
            return false;
        } else {
            return true;
        }
    }
}

Adding a new user
Code:
$newuser = &$kreator->create('user');
$newuser->set_password('temppassword');
$newuser->set_name('some random name');
$newuser->save();


Getting some users
Code:
//passing a sql query instead of an id)
$userlist=&$kreator->get('user','WHERE `name` LIKE `a%`');
for($i=0, $maxi=count($userlist)-1;$i<=$maxi;$i++)
{
  echo $i,'<b>',$userlist[$i]->data['name'].'</b><br>';
}

Getting the user with id 10
Code:
$user = &$kreator->get('user',10);

Assigning the user to a group
Code:
$group = &$kreator->get('group','WHERE `name`="root"');

// simple linkage
$user->link_to($group);

// or you can create multiple types of links between objects
// suppose $car is a car object,
$user->link_to($car, 'owner');
$user->link_to($car, 'driver');
...

Deletion of the above user
Code:
$user->delete();

This system just provides basic data-to-object and back transformations, while doing caching on already loaded data (only loads new or changed data from the database).

I have the following classes
* rdp_db (general database abstraction)
* rdp_db__mysql ( mysql implementation)
* rdp_crud_creator (object manager)
* rdp_crud_creator__mysql (mysql-optimized)
* rdp_crud (basic object) - extends into obj_user, obj_car, obj_group, etc...

To integrate this with CI i have two options
1. Put everything as a library
2. Put all the object definitions (like the above-mentioned user class) in the models folder and the rest in the library

Every object manager needs to be instanced only once.. and here i can do it in two ways (again Wink):
1. Use a function with $manager=&get;_obj_manager($name=''); which returns one of the instances (i need a $name parameter since i can have more than one)
2. Set it up in the controller with $this->dbm[$name]=&$rdp_crud after creating an instance

Since CI database classes aren't meant to be extended/etc (hope i didn't read it the wrong way on the user guide), should i also use another connection to the database (with the same settings), just to be on the safe side that those two won't mess with each other?

I know this is a little different from the usual mvc implementation but i think it's scalable, simple to use and extend and that`s what matters in the end.

Id like your input on the best method of integration.




Theme © iAndrew 2016 - Forum software by © MyBB