Wednesday, 10 October 2018

Default Custom Model in CodeIgniter

Assalamualaikum dan salam sejahtera..

Untuk memudahkan pembangunan sistem menggunakan CodeIgniter, penggunaan default Model amatlah digalakkan kerana ianya lebih tersusun selagi mana struktur data tidak terlalu kompleks.

Default Model ini juga boleh diubahsuai mengikut keperluan semasa. Default model ini perlu dimasukkan dalam satu page yang dikenali sebagai MyModel.php.

Contoh Code Dalam Default Model:

1. Untuk mendapatkan satu field

Controller:
$data = $this->MyModel->get_one_field('FirstName','NRIC = '700101017254','User');

Model:

function get_one_field($col,$where,$table)
{
   $this->db->select($col);
   $this->db->where($where);
   $query = $this->db->get($table);
   return $query->row_array();
}

2. Untuk mendapatkan set data

Controller:
$data = $this->MyModel->get_data('User');

Model:
function get_data($table)
{
   $this->db->select('*');
   $this->db->from($table);
   $query = $this->db->get();
   return $query->result_array();
}

3. Untuk mendapatkan satu set data

Controller:
$data = $this->MyModel->get_info('User', array('NRIC'=>'700101017254', 'Active'=>'Y'));

Model:
function get_info($table,$where=0)
{
    $this->db->select('*');
    $this->db->from($table);
    if($where != 0)
    {
      foreach($where as $key=>$val){
         $this->db->where($key, $val);
      } 
    }
    $query = $this->db->get();
    return $query->row_array();
}

4. Untuk membuat pengiraan data

Controller:
$data = $this->MyModel->get_count('User', array('Active'=>'Y','Gender'=>'F'));

Model:
function get_count($table,$where)
{  
    $this->db->from($table);
    $this->db->where($where);
    return $this->db->count_all_results();
}

5. Untuk mendapatkan jumlah

Controller:
$data = $this->MyModel->get_sum('User', 'Fee', array('Active'=>'Y','Gender'=>'F'));

Model:
function get_sum($table,$field,$where)
{
    $this->db->select_sum($field);
    $this->db->where($where);
    $query = $this->db->get($table);  
    return $query->row_array();     
}

6. Untuk membuat list dropdown

Controller:
$data = $this->MyModel->get_select_list('Title',array('key'=>'id','val'=>'description','orderby'=>'id'),1, array('Active'=>'Y'));

Model:
function get_select_list($table,$cols=array('key' => 'id','val' => 'name', 'orderby' => 'x'),$with_select=1, $where='x')
{
    extract($cols);
    if($orderby=='x'){
       $this->db->order_by($val);
    }
    if($where!='x')
       $this->db->where($where);
       $query = $this->db->get($table);
       $arr = $query->result_array();
    if ($with_select) $data[''] = '-- Select --';
       foreach ($arr as $k => $v){
          extract($v);
          $data[$$key] = $$val;
       }
    return $data;
}

7. Join Table

Controller:
$data = $this->MyModel->get_valueJoin('Title','Title.id = User.title',array('Gender'=>'M'),'FirstName', 'User');

Model:
function get_valueJoin($join,$join1,$where,$field,$table)
{
    $this->db->join($join,$join1);
    $this->db->where($where);
    $this->db->order_by($field, "asc");
    $query = $this->db->get($table);
    return $query->result_array();
}

8. Insert Data

Controller:
//post data
$data['user']['title'] = $this->input->post('title');
$data['user']['name'] = $this->input->post('name');
$data['user']['dob'] = date('Y-m-d',strtotime($this->input->post('dob')));
$data['user']['gender'] = $this->input->post('gender');
$data['user']['race'] = $this->input->post('race');

$data = $this->MyModel->insert_data('User',$data['user']);

Model:
function insert_data($table,$data)
{
    if ($this->db->insert($table, $data)) {
       return $this->db->insert_id();
    }
    else return false;
}

9. Update Data

Controller:
$user = $this->MyModel->get_info('User', array('NRIC'=>'700101017254', 'Active'=>'Y'));

//post data
$data['user']['title'] = $this->input->post('title');
$data['user']['name'] = $this->input->post('name');
$data['user']['dob'] = date('Y-m-d',strtotime($this->input->post('dob')));
$data['user']['gender'] = $this->input->post('gender');
$data['user']['race'] = $this->input->post('race');

$data = $this->MyModel->update_data('User',$user['id'],$data['user']);

Model:
function update_data($table,$key,$data)
{
    $this->db->where($key);
    $this->db->update($table, $data);
}

10. Delete data

Controller:
$data = $this->MyModel->delete_data('User',array('NRIC'=>'700101017254'));

Model:
function delete_data($table,$where=0)
{
   if($where != 0)
   {
      foreach($where as $key=>$val){
         $this->db->where($key, $val);
      } 
   }
   $this->db->delete($table);
}


*******************************
Disediakan Oleh :
Haslina Shamsudin
PPTMK
Unit Pengaturcaraan

0 comments:

Post a Comment