函数 CI_DB_query_builder::join() 的参数太少,在 PHP(Codeigniter) 中使用 join table 传递了 1 个,预计至少有 2 个

Too few arguments to function CI_DB_query_builder::join(), 1 passed and at least 2 expected using join table in PHP(Codeigniter)

我正在尝试减少模型中的所有方法,因此我决定使其成为动态的。已完成为插入、更新、获取、删除创建动态,但在为加入 2 tables 创建动态时遇到问题。

遇到错误:

"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"`

注意:在这个项目中,我正在做的模块是 "following" 用户可以在其中关注特定的人(如 instagram)。我有 2 个 tables 命名为:users and following。

在我的 "following table" 中,我的列是: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 个人点击关注按钮后,在我的 table 关注中将如下所示:

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

评论部分有效,但我想将其设为动态。

问题:table如何制作动态加入方法?

希望对您有所帮助:

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