开发者

How can I merge two Doctrine records without null values overwriting existing values?

开发者 https://www.devze.com 2022-12-29 05:13 出处:网络
I have an array of default values that I\'d like to merge with a Doctrine record. I tried using Doctrine\'s merge method but it\'s overwriting existing values with the merge array, even if th开发者_St

I have an array of default values that I'd like to merge with a Doctrine record. I tried using Doctrine's merge method but it's overwriting existing values with the merge array, even if th开发者_StackOverflow社区e merge array contains null values. I'd like to merge in a way that only null or empty values are replaced with existing default values.


Try this:

    $yourRecord = new YourRecordModel();
    $yourRecord->assignIdentifier(123); // ID of the record to update
    foreach ($yourArray as $key=>$value)
    {
        if (!empty($value))
        {
            $yourRecord[$key] = $value;
        }
    }
    $yourRecord->save();
0

精彩评论

暂无评论...
验证码 换一张
取 消