【问题标题】:Codeigniter Active Record: Inserting empty records into multiple tables based on an idCodeigniter Active Record:根据一个id将空记录插入到多个表中
【发布时间】:2013-06-12 06:00:34
【问题描述】:

我正在使用 Codeigniter Active Record 类

我正在尝试在多个表中为特定 ID 创建空记录。例如,假设我的用户的 ID 为 $user_id,而我想要 create empty records in tables T1, T2 and T3。我遇到问题的代码是:

        // Store the select statement in cache
        $this->db->start_cache();
        $this->db->set('user_id', $user_id);
        $this->db->stop_cache();
        // Insert empty record in tables related to user
        $this->db->insert('T1');
        $this->db->insert('T2');
        $this->db->insert('T3');
        $this->db->flush_cache();

在表 T1 中插入了一条空记录,但随后出现错误:

您必须使用“set”方法来更新条目。

编辑: - 显然,表 T1, T2 & T3user_id 列。

我做错了什么,我该如何解决?我是 codeigniter 的新手,CI 文档对此问题并不清楚。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您可以将所需的值存储在数组中,然后进行插入,而不是使用$this->db->set()。这可能会有所帮助

        // Store the select statement in cache
        $data = array("user_id"=>$user_id);
    
        // Insert empty record in tables related to user
        $this->db->insert('T1',$data);
        $this->db->insert('T2',$data);
        $this->db->insert('T3',$data);
    

    【讨论】:

    • 你好好奇编码器。非常感谢您的回复!这正是我一直在寻找的。有效且优雅:) 再次感谢! :)
    【解决方案2】:

    试试这个

        // Store the select statement in cache
        $this->db->cache_on();
        $this->db->set('user_id', $user_id);
        $this->db->cache_off();
        // Insert empty record in tables related to user
        $this->db->insert('T1');
        //$this->db->set('user_id', $user_id);
        $this->db->insert('T2');
        //$this->db->set('user_id', $user_id);
        $this->db->insert('T3');
        $this->db->cache_delete_all();
    

    【讨论】:

    • 嗨丹麦人,这当然可以,但我早先缓存了$this->db->set('user_id', $user_id); 的声明,以避免重复此声明:(。想知道是否有更优雅的方法来做到这一点?跨度>
    • 嗨,丹麦人,你的新解决方案也不起作用:(。不过,请查看下面来自奇怪编码器的解决方案。我确实学到了一些新东西。无论如何,非常感谢你的帮助。: )
    猜你喜欢
    • 2013-01-11
    • 2013-02-25
    • 2015-01-04
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    相关资源
    最近更新 更多