Skip to content
Advertisement

Make A Dynamical Model With Join CodeIgniter

I asked this question yesterday but it got deleted because admins said it got similar question with other post but when I check it there it doesn’t have the same problem with me, so ill ask again and hope that admin can understand.

I have this model in CodeIgniter:

public function findJoinGroup($select,$where,$table,$join,$groupby)
{
    $this->db->select($select);
    $this->db->from($table);
    $this->db->join($join);
    $this->db->where($where);
    $this->db->group_by($groupby);
    return $this->db->get()->result_array();
}

It has a parameter that I can use dynamically (I mean I can use this model with different value based on controller) – right? So when I use controller it will be like this:

$select = array(
            'ta.TR_APP_ID', 
            'ta.TR_SUB_DATE', 
            'ta.RB_ID', 
            'tr.RB_TOTAL', 
            'count(tdr.DET_RB_ID) as JUMLAH_ITEM', 
            'ME.EMP_FULL_NAME'
        );
$join = array(
            'tr_reimburse tr', 'tr.RB_ID = ta.RB_ID',
            'tr_detail_reimburse tdr', 'tdr.RB_ID = tr.RB_ID ',
            'm_employee me', 'tr.EMP_ID = me.EMP_ID',
        );
$where = array(
            'EMP_ID' => $empID,
            'TR_APP_STATUS ' => '0',
        );
$groupby = 'ta.TR_APP_ID';
$data['app'] = $this->Model_online->findJoinGroup($select,$where,'tr_approval ta',$join,$groupby);

I can set the value of select whatever I want, I can set the value of where to whatever I want, etc

But there is a problem: when I set the value of join, it doesn’t work like where and select.

I get this error:

An uncaught Exception was encountered
Type: ArgumentCountError

Message: Too few arguments to function CI_DB_query_builder::join(), 1 passed in C:laragonwwwonlineformapplicationmodelsModel_online.php on line 71 and at least 2 expected

Filename: C:laragonwwwonlineformsystemdatabaseDB_query_builder.php

Line Number: 526

My question is how to set this array value in so I can use it in the model inside the variable join

So my model can be used for many situation that need a join (single or multiple join whatever)

$join = array(
            'tr_reimburse tr', 'tr.RB_ID = ta.RB_ID',
            'tr_detail_reimburse tdr', 'tdr.RB_ID = tr.RB_ID ',
            'm_employee me', 'tr.EMP_ID = me.EMP_ID',
        );

I don’t want make model like this, because it means it only can be used by 4 join, when I need a 3/2/1 join I make model again.

$this->db->select('*');

$this->db->from('table1');

$this->db->join('table2','table1.id=table2.id'); 

$this->db->join('table3','table2.id=table3.id');

$this->db->join('table4','table3.id=table4.id'); 

$this->db->join('table5','table5.id=table4.id');

Sorry I explain a lot, because I can’t find solution of this, and I don’t want admin delete my unanswered post 🙁

Please don’t delete this admin let people help me :’)

Advertisement

Answer

Use multidimensional array.I think this will works for you.

$join = array(
        array('tr_reimburse tr', 'tr.RB_ID = ta.RB_ID'),
        array('tr_detail_reimburse tdr', 'tdr.RB_ID = tr.RB_ID'),
        array('m_employee me', 'tr.EMP_ID = me.EMP_ID'),
    );

and use this in model side like this.

if(!empty($join)):
    foreach($join as $j):
      $this->db->join($j);
    endforeach;
endif;
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement