/** * Merge this model with another one of the same type. * * All attributes of the given model will be applied to this model if they are empty. Textarea's will be concatenated. * All links will be moved to this model. * Finally the given model will be deleted. * * @param ActiveRecord $model */ public function mergeWith(ActiveRecord $model, $mergeAttributes = true, $deleteModel = true) { if (!$this instanceof \GO\Customfields\Model\AbstractCustomFieldsRecord && $model->id == $this->id && $this->className() == $model->className()) { return false; } //copy attributes if models are of the same type. if ($mergeAttributes) { $attributes = $model->getAttributes('raw'); //don't copy primary key if (is_array($this->primaryKey())) { foreach ($this->primaryKey() as $field) { unset($attributes[$field]); } } else { unset($attributes[$this->primaryKey()]); } unset($attributes['files_folder_id']); foreach ($attributes as $name => $value) { $isset = isset($this->columns[$name]); if ($isset && !empty($value)) { if ($this->columns[$name]['gotype'] == 'textarea') { $this->{$name} .= "\n\n-- merge --\n\n" . $value; } elseif ($this->columns[$name]['gotype'] = 'date' && $value == '0000-00-00') { $this->{$name} = ""; } elseif (empty($this->{$name})) { $this->{$name} = $value; } } } $this->save(); //copy custom fields if ($model->customfieldsRecord) { $this->customfieldsRecord->mergeWith($model->customfieldsRecord, $mergeAttributes, $deleteModel); } } $model->copyLinks($this); //move files. if ($deleteModel) { $this->_moveFiles($model); $this->_moveComments($model); } else { $this->_copyFiles($model); $this->_copyComments($model); } $this->afterMergeWith($model); if ($deleteModel) { $model->delete(); } }