【问题标题】:Join and Update in CodeIgniter在 CodeIgniter 中加入和更新
【发布时间】:2018-02-16 06:21:24
【问题描述】:

我的数据库中有一个 users 表,我想与另外两个表(cmets、posts)建立关系。

当我发表评论时,它会根据 id 及其用户名发布;帖子也是如此,如果用户发布帖子,其用户名进入数据库,现在假设该用户想要更新他的信息,例如他的电子邮件、网站、头像 img 等。

其他两个表中的所有新数据都应根据 users.id/user_id 自行更新。我确定这与加入有关,我实际上正在这样做,但什么也没发生。

这是我的代码:

public function update($id, $data)
{
    $this->db->select('users.*');
    $this->db->from('users');
    $this->db->join('posts', 'posts.username = users.username','inner');
    $this->db->join('posts', 'posts.user_id = users.id','inner');
    $this->db->join('comments', 'comments.user_id = users.id','inner');
    $this->db->join('comments', 'comments.username = users.username','inner');
    $this->db->join('comments', 'comments.avatar_img = users.avatar_img','inner');
    $this->db->join('comments', 'comments.email = users.email','inner');
    $this->db->join('comments', 'comments.website = users.website','inner');
    // Update info in all tables according to user id.
    $this->db->where('id', $id);

    $query = $this->db->get();

    $this->db->update($this->table, $data);
}

您可以在上面的代码中观察到,我正在尝试从用户表中选择所有内容,并且我正在尝试将其与另一个表连接起来以更新其信息。

希望大家能帮帮我,谢谢。

这是我尝试加入的三个表 一些用户表

帖子表

评论表

【问题讨论】:

  • 我很好奇,那有什么作用?你必须让“某事”发生!
  • $data 里有什么?你能分享一个例子吗?为什么还要在执行更新时执行 Select 查询?
  • 单独更新每个表数据

标签: php codeigniter join codeigniter-3


【解决方案1】:

将所有信息存储在所有表中是否有特定原因?

将个人数据存储在用户表中,将与帖子相关的数据存储在帖子表中,将评论的数据存储在评论表中不是更容易吗?

当您显示帖子时,您会从用户表中获取用户数据。

所以没有必要更新你想要更新的方式。

如果您想坚持自己的数据存储方式,我认为您想得太远了。您不需要连接即可更新数据。只需使用您发送给函数的数据分别更新每个表。

意思:

$this->db->where('user_id', $id);
$this->db->update('users', $data);

$this->db->where('user_id', $id);
$this->db->update('posts', $data);

$this->db->where('user_id', $id);
$this->db->update('comments', $data);

【讨论】:

    【解决方案2】:

    你的控制器

     public function update_data()
    {
        //Use $id and $data according to your database table's requirements 
        $update_user = $this->model-name->update_user($id, $data);
        $update_post = $this->model-name->update_post($id, $data);
        $update_comments = $this->model-name->update_comments($id, $data);
        if($update_user )
        {
          //Do what you want
        }
         if($update_post )
        {
          //Do what you want
        }
         if($update_comments )
        {
          //Do what you want
        }
    }
    

    你的模型

    public function update_user($id, $data)
    { 
      $this->db->where('id', $id);
      return $this->db->update('users', $data);
     }
    
    public function update_post($id, $data)
    { 
      $this->db->where('id', $id);
      return $this->db->update('posts', $data);
     }
    
      public function update_comments($id, $data)
    { 
      $this->db->where('id', $id);
      return $this->db->update('comments', $data);
     }
    

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      相关资源
      最近更新 更多