refactoring createOrUpdate method of models

This commit is contained in:
Amine Ben hammou
2018-02-07 17:40:06 +00:00
parent a2270d6689
commit 91622f8cd9
11 changed files with 69 additions and 153 deletions

View File

@@ -127,25 +127,10 @@ class CustomField extends Model {
}
static function createOrUpdate($data = array()) {
$custom_field = false;
if(isset($data['id']) && (int)$data['id'] > 0) {
$custom_field = self::findOne((int)$data['id']);
}
// set name as label by default
if(empty($data['params']['label']) && isset($data['name'])) {
$data['params']['label'] = $data['name'];
}
if($custom_field === false) {
$custom_field = self::create();
$custom_field->hydrate($data);
} else {
unset($data['id']);
$custom_field->set($data);
}
return $custom_field->save();
return parent::internalCreateOrUpdate($data);
}
}