简体   繁体   English

Batch_update主键联接表

[英]Batch_update primary key joining table

i'm having a problem with batch update function with codeigniter, i'm using a joining table for my products and categories, as I have a many to many relationship. 我在使用Codeigniter进行批处理更新功能时遇到问题,我在为我的产品和类别使用联接表,因为我与多对多关系密切。 I have searched high and low for an answer but still nothing so i have come here to ask more senior technicians for help. 我一直在寻找答案,但仍然一无所获,所以我来这里向更多高级技术人员寻求帮助。

I have 2 columns in a table "product_category" with "product_id" & "category_id" so I can link 1 product to many categories. 我在带有“ product_id”和“ category_id”的“ product_category”表中有2列,因此我可以将1个产品链接到许多类别。 I have managed to perform the insert query which inserts all the id's fine, but the update is not working. 我设法执行了插入查询,该插入查询可以很好地插入所有ID,但是更新不起作用。 Here's my code: 这是我的代码:

model: 模型:

 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'); } 

array: 数组:

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

My error code: 我的错误代码:

Error Number: 1062 错误号:1062

Duplicate entry '327-3' for key 'PRIMARY' 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') Any help would be greatly appreciated: 重复条目'327-3'的键'主要' 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')任何帮助将不胜感激:

thanks 谢谢

I'm not 100% sure this is your entire problem, but it looks like the product_category table has product_id set as a primary key - where it looks like you are trying to enter many identical product_id's. 我不是100%肯定这是您的整个问题,但是看起来product_category表中的product_id设置为主键-好像您试图输入许多相同的product_id一样。 If that were the case you should probably just be using a regular update not batch, not to mention removing/replacing the primary key. 如果是这种情况,您可能应该只使用常规更新而不是批量更新,更不用说删除/替换主键了。

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

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