简体   繁体   English

如何合并两个Doctrine记录而没有空值覆盖现有值?

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

I have an array of default values that I'd like to merge with a Doctrine record. 我有一个默认值数组,希望将其与Doctrine记录合并。 I tried using Doctrine's merge method but it's overwriting existing values with the merge array, even if the merge array contains null values. 我尝试使用Doctrine的merge方法,但是即使合并数组包含空值,它也会用merge数组覆盖现有值。 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();

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

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