0

I am trying to reduce all my methods in my model so I decided to make it a dynamic. Done in creating a dynamic for insert,update,fetch,delete but having a problem in creating a dynamic for joining 2 tables.

Error encountered:

"Too few arguments to function CI_DB_query_builder::join(), 1 passed in C:\xampp\htdocs\snapmatic\application\models\Crud_model.php on line 130 and at least 2 expected"`

NOTE: In this project, the module i'm doing is "following" where a user can follow a specific person(like instagram). I have 2 tables named: users and following.

In my "following table", my columns are: id, user_id and user_following. The user_id is where the account logged in and the user_following is the one you've followed.

Scenario: In my table users, you have 2 data: Person 1 and Person 2
Person 1 account is logged in then Person 1 followed Person 2.

After Person 1 clicked the button follow, in my table of following will look like this:

id: 1 user_id: 1 user_follow:2

THIS IS MY Controller

$id = $this->session->user_id;
$where = array('following.user_id => $id');
$join  = array('following,following.user_following = users.id');
$fetch_following = $this->Crud_model->join_table('*','users',$where,$join);

//Also tried these
//$where = "('following.user_id', $id)";
//$where = "'following.user_id', $id)";
//$where = "('following.user_id, $id')";
//$where = "'following.user_id, $id'";
//$join  = "'following,following.user_following = users.id'";
//$join  = "('following,following.user_following = users.id')";
//$join  = "('following','following.user_following' = 'users.id')";

Model

public function join_table($tag,$table,$where,$join){
    // public function join_table($id){

        $this->db->select($tag);
        $this->db->from($table);
        $this->db->join($join);

        $this->db->where($where);
        // $this->db->select('*');
        // $this->db->from('users');
        // // $this->db->group_by('invoice_number'); 
        // $this->db->join('following','following.user_following = users.id');
        // $this->db->where('following.user_id', $id);
        $result = $this->db->get();
        return $result->result();
    }

The comment part is working but I would like to make it a dynamic.

Question: How to make a dynamic method for joining a table?

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Angel
  • 966
  • 4
  • 25
  • 60
  • I suppose it is `$join = array('following' ,'following.user_following = users.id');` See the __quotes__ which make your array an array with 2 elements? – u_mulder Jul 04 '18 at 11:52
  • Actually sir, i also tried that but having a same error. – Angel Jul 04 '18 at 12:00

1 Answers1

2

Hope this will help you :

keep join table and on condition into two different variable and $where should be an array with key value pair like this :

Your controller should be like this :

$id = $this->session->user_id;
$where = array('following.user_id' => $id);
$join_table = 'following';
$join_on = 'following.user_following = users.id';

$fetch_following = $this->join_table('*','users',$where,$join_table,$join_on);

Your join_table method should be like this :

public function join_table($tag,$table,$where,$join_table,$join_on)
{
  $this->db->select($tag);
  $this->db->from($table);
  $this->db->join($join_table,$join_on);

  $this->db->where($where);  
  $query = $this->db->get();
  return $query->result();
}

For more : https://www.codeigniter.com/user_guide/database/query_builder.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34