• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
batch update on primary key?

#1
[eluser]ppwalks[/eluser]
Hi all,I'm trying to use CL native batch update function, but for the purpose I am trying it does not like it. I am trying to update where product_id = product_id, it is in a joining table so it can occur several times.

Here's my array:

Code:
Array ( [0] => Array ( [product_id] => 327 [category_id] => 3 ) [1] => Array ( [product_id] => 327 [category_id] => 5 ) [2] => Array ( [product_id] => 327 [category_id] => 7 ) )


The error code says:

Error Number: 1062

Duplicate entry '327-3' for key 'PRIMARY'
Code:
UPDATE `product_category` SET `category_id` = CASE WHEN `product_id` = '327' THEN '3' WHEN `product_id` = '327' THEN '5' WHEN `product_id` = '327' THEN '7' ELSE `category_id` END WHERE `product_id` IN ('327','327','327')


And my model:
Code:
function update_product_cat($product, $cat_id) {  
     $data = array();
     foreach( $product as $index => $value )
      {
        $data[] = array(
            'product_id'    => $value ,
            'category_id'  => $cat_id[ $index ]
         );
       }
    
     $this->db->update_batch('product_category', $data, 'product_id');

Why will it not update the table, I have already double checked and both columns are (int) which is correct?

Any help to point me in the right direction would be greatly appreciated.

Thanks

}

#2
[eluser]ppwalks[/eluser]
Solved, and it was so obvious as well, I simply deleted the records then inserted the updated one with the same function I used for the original insert...

Thanks anyway


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.