Implementasi Multiple Database dengan CodeIgniter (CI)

Contoh bentuk sistem yang menerapkan multiple database dapat Anda lihat di Simple Blog Engine System.

Masih berhubungan dengan sistem tersebut, berikut cara kerja databasenya.

1.  Koneksi Master Database

Koneksi dengan master database merupakan inti dari aliran data blog engine. Oleh karena itu, koneksi dengan database ini wajib dilakukan di saat pengaksesan website. Jika dalam gaya programming PHP biasa, setiap aksi yang mengikutkan database harus meng-include file koneksi.inc atau sejenisnya. Hal ini tidak perlu lagi dilakukan di CI, karena CI menawarkan fasilitas ‘autoload’ yang otomatis akan menjalankan proses yang diinisialisasi di autoload.

Konfigurasi default database awal dilakukan di system/application/config/database.php. Ubah bagian inisialisasi array seperti berikut ini :
$db['default']['hostname'] = "localhost";

$db['default']['username'] = "root";

$db['default']['password'] = "mypassw123";

$db['default']['database'] = "blo99y";

$db['default']['dbdriver'] = "postgre"

Untuk setting autoload database, buka file system/application/config/autoload.php dan tambah :

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

2.  Pembuatan Database Member

Proses ini terjadi bersamaan dengan registrasi member. Berikut proses kerja yang terjadi :

flow_chart_db_member

Gambar 1. Flowchart pembuatan database member

Source code proses pada Controller :

function save()
{
$rules = array(
array(
'field' => 'mname',
'label' => 'Name',
'rules' => 'required|min_length[3]|max_length[255]'
),
array(
'field' => 'uname',
'label' => 'Your Email',
'rules' => 'required|valid_email|max_length[255]'
),
array(
'field' => 'passw',
'label' => 'Your Blo99y Password',
'rules' => 'required|matches[cpassw]|min_length[5]'
),
array(
'field' => 'cpassw',
'label' => 'Confirm Password',
'rules' => 'required|min_length[5]'
),
array(
'field' => 'blog_name',
'label' => 'Blog\'s Name',
'rules' => 'required|max_length[50]'
),
array(
'field' => 'motto',
'label' => 'Motto',
'rules' => 'required|max_length[100]'
),
array(
'field' => 'url_blog',
'label' => 'Your Blog URL',
'rules' => 'required|max_length[255]|callback_url_check'
)
);

$this->form_validation->set_rules($rules);

if($this->form_validation->run()==FALSE){
$this->load->view('vreg');
$this->load->view('footer');
}else{
//adding member
$this->load->model('regModel');
$row = $this->regModel->insert_entry();
//get member_id
$id = $row->id;
$success_text=$this->create_db_user($id,$this->input->post('mname'));
if($success_text!=false)
$this->session->set_flashdata(array('success'=>true,'success_text'=>$success_text));
else
$this->session->set_flashdata(array('error'=>true,'error_text'=>'Sorry, there\'re some troubles appear on processing your account.'));

$this->session->set_flashdata($_POST);
redirect('home');
}
}

function url_check($str){
$this->load->model('gen_setting'); $id='';
if($this->gen_setting->is_url_exist($str,$id)){
$this->form_validation->set_message('url_check', 'The %s is not available. It is already exist');
return false;
}else
return true;
}

Fungsi create_db_user :
function create_db_user($id,$name)
{
//substring dbase_name
$mydb = 'db_'.$id;
//echo 'mydbase name = '.$mydb;

//create user's dbase
if ($this->dbforge->create_database($mydb))
{
$dsn = 'postgre://root:yorukochupi2@localhost/'.$mydb.'?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache';
//$dsn = 'postgre://root:yorukochupi2@localhost/'.$mydb;
$UDB = $this->load->database($dsn,true);

//generate db_schema
$UDB->query('create sequence blog_idb_seq');
$UDB->query("create table blog (idb integer DEFAULT nextval('blog_idb_seq') PRIMARY KEY NOT NULL,idcat integer NOT NULL,title character varying(255),subtitle text,content text,createdin timestamp,createdby character varying(100),bstatus character varying(50))");
$UDB->query('create sequence category_idcat_seq');
$UDB->query("create table category (idcat integer DEFAULT nextval('category_idcat_seq') PRIMARY KEY NOT NULL ,category character varying(100))");
$UDB->query('create sequence comments_idc_seq');
$UDB->query("create table comments (idc integer DEFAULT nextval('comments_idc_seq') PRIMARY KEY NOT NULL,idb integer NOT NULL,name character varying(100), email character varying(255),content text,date timestamp)");

//predefined
//saving blog name and motto
$this->load->model('gen_setting');
$this->gen_setting->predefined($id);
//saving default profile w/ photo
$this->load->model('profile');
$this->profile->default_profile($id,$name);

//generate default category -> general
$this->load->model('category');
$this->category->precategory($id);

//generate default post
$this->load->model('blog');
$this->blog->insert($id);

$success_text = ' You successfully join Bloggy. Now you can login here.';
return $success_text;
}else{
return false;
}
}

Pengujian pembentukan database member:

registration_blog_form

Gambar 2. Formulir halaman registrasi

registration_success_report

Gambar 3. Tampilan pesan saat sukses registrasi

generate_db

Gambar 4. Hasil registrasi dan pembangkitan database member

3 thoughts on “Implementasi Multiple Database dengan CodeIgniter (CI)

    • Multiple database… menurut saya.. diperlukan saat suatu sistem dianggap ga mampu dihandle 1 database. Dengan pertimbangan databasenya akan terus menerus membesar… Contohnya blog engine seperti wordpress ini (usernya slalu bertambah.. isi blog pribadi mrk juga bkembang trus menerus)..
      Dan di saat yang sama frekuensi load isi database juga sering dilakukan.. jadi, untuk optimasi sistem saya (saat itu sedang coba bikin blog engine sederhana).. saya menerapkan multiple dbase ^ ^

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s