参数CI_DB_query_builder :: join()的参数太少,传递了1个参数,并且使用PHP(Codeigniter)中的连接表期望了至少2个参数

时间:2018-07-04 11:49:43

标签: php codeigniter codeigniter-3 codeigniter-query-builder

我正在尝试减少模型中的所有方法,因此我决定使其成为动态模型。在创建用于插入,更新,提取,删除的动态时完成,但是在创建用于连接两个表的动态时遇到问题。

遇到错误:

  

“函数CI_DB_query_builder :: join()的参数太少,传递了1个   在C:\ xampp \ htdocs \ snapmatic \ application \ models \ Crud_model.php中   130,预计至少2”“

注意:在这个项目中,我正在执行的模块是“关注”,用户可以在其中关注特定的人(例如instagram)。我有2个表,分别命名为:users及以下。

在我的“下面的表”中,我的列是:id,user_id和user_following。 user_id是登录的帐户,user_following是您遵循的帐户。

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.

在“人员1”单击“关注”按钮之后,我的关注表将如下所示:

id: 1 user_id: 1 user_follow:2

这是我的控制者

$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')";

模型

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();
    }

评论部分正在运行,但我想使其具有动态感。

问题:如何制作一种动态的联接表方法?

1 个答案:

答案 0 :(得分:1)

希望这对您有帮助:

join tableon条件保留到两个不同的变量中,并且$where应该是具有键值对的数组,如下所示:

您的控制器应如下所示:

$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);

您的join_table方法应如下所示:

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();
}

更多信息:https://www.codeigniter.com/user_guide/database/query_builder.html