Orm Yii2更新条目(如果存在)

Orm Yii2更新条目(如果存在),orm,yii2,yii2-model,Orm,Yii2,Yii2 Model,前面定义的项$title。我想要如果找到$find_title,用$title中的所有字段更新它。否则,创建一个新对象 $find_title = Title::find()->where(["upc" => $title->upc])->one(); if ($find_title != null) { $title->id = $find_title-

前面定义的项$title。我想要如果找到$find_title,用$title中的所有字段更新它。否则,创建一个新对象

  $find_title = Title::find()->where(["upc" => $title->upc])->one();
                    if ($find_title != null) {
                        $title->id = $find_title->id;
                        $title->save();
                    } else {
                        $title->save();
                    }
它在拉雷维尔起作用了

$find_title = Title::find()->where(["upc" => $title->upc])->one();
if ($find_title != null) {
    $id = $title->id;
    $find_title->attributes = $title->attributes;
    $find_title->id = $id;
    $find_title->save();
} else {
    $title->save();
}
在这里,您将
$title
的所有属性分配给
$find\u title
,还原
id
,然后保存