Codeigniter update_batch is not working

2019-09-02 07:29发布

问题:

I have a db table:

ConfigID | Type  | Key            | Value
--------------------------------------------------------------
0        | "API" | "ClientID"     | "iofoewi"
1        | "API" | "ClientSecret" | "eijfoiewjfioejfoiewjfoie"

Take the following code:

    $data = array(
        array(
            'Key' => "ClientID",
            'Value' => $testAPICredential->ClientID
        ),
        array(
            'Key' => "ClientSecret",
            'Value' => $testAPICredential->ClientSecret
        )
    );

    try
    {
        $this->context->db->trans_start();
        $this->context->db->update_batch( $this->tableName, $data, "Key" ); 
        $this->context->db->trans_complete();        
        return ($this->context->db->trans_status() === FALSE)? FALSE:TRUE;
    }
    catch( Exception $e )
    {
        return FALSE;
    }

It outputs this SQL when I use the profiler:

UPDATE `config` SET `Value` = CASE 
WHEN `Key` = 'ClientID' THEN 'iofoewi2'
WHEN `Key` = 'ClientSecret' THEN 'eijfoiewjfioejfoiewjfoie2'
ELSE `Value` END WHERE `Key` IN ('ClientID','ClientSecret')  

And yet the database table is un=touched?

Any ideas?

回答1:

If you are using CI 3.1.2 maybe it's will help you.

In our project. We find out that function update_patch sometimes will be have problem. Index for update sometimes will be wrong. So i found out another function for replace is update_batch_real_index In file /yourproject/system/database/DB_query_builder.php

From time using this to present is 3 months we not found any bug again. Maybe you should try that.