Home > PHP Framework > YII > body text

How to batch update data in Yii

王林
Release: 2020-02-27 14:34:32
Original
3761 people have browsed it

How to batch update data in Yii

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,
    ]
}
Copy after login

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!

Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!