Requirements:
Send multiple pieces of data to the background, some of which already exist in records, and only the values of some of their fields need to be modified.
(Recommended tutorial: yii framework)
Solution:
The code is as follows:
foreach($goods as $k => $v) { if(yourModel::updateAllCounters( ['goods_num' => $v], ['goods_id' => $k,'user_id' => $id] )) { continue;//如果已经更新,则跳过此次循环,到下一次 } $data[] = [ 'user_id' => $id, 'goods_id' => $k, 'goods_num' => $v, 'created_time' => $time, ] }
More programming related content, Please pay attention to the Introduction to Programming column on the php Chinese website!
The above is the detailed content of How to batch update data in Yii. For more information, please follow other related articles on the PHP Chinese website!