简体   繁体   English

Codeigniter活动记录:根据ID将空记录插入到多个表中

[英]Codeigniter Active Record: Inserting empty records into multiple tables based on an id

I am using Codeigniter Active Record Class 我正在使用Codeigniter Active Record类

I am trying to create empty records for a particular id in multiple tables . 我正在尝试为多个表中的特定ID创建空记录 For example lets say my user has the id $user_id , and I want to create empty records in tables T1, T2 and T3 . 例如,假设我的用户的ID $user_id ,我想create empty records in tables T1, T2 and T3 The code I am having trouble with is: 我遇到麻烦的代码是:

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

An empty record is inserted in table T1 but then I get the error: 在表T1插入了空记录,但随后出现错误:

You must use the "set" method to update an entry. 您必须使用“设置”方法来更新条目。

EDIT: - Obviously, tables T1, T2 & T3 have user_id columns. 编辑: -显然,表T1, T2 & T3具有user_id列。

What am I doing wrong and how can I fix this? 我在做什么错,我该如何解决? I am new to codeigniter and the CI documentation is not clear about this issue. 我是Codeigniter的新手,并且CI文档对此问题尚不清楚。

Instead of using $this->db->set() , you can store the required value in an array and then do the insertion. 您可以将所需的值存储在数组中,然后执行插入操作,而不是使用$this->db->set() This may help 这可能会有所帮助

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

Try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM