Home >PHP Framework >YII >How to batch update data in Yii

How to batch update data in Yii

王林
王林Original
2020-02-27 14:34:323819browse

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,
    ]
}

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!

Statement:
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