/** * Copy attributes from one model to another. If the attributes are relations, then only copy when it is a * HAS_ONE variant. In the case that the relation is an OwnedModel, take special consideration for CurrencyValue * CustomField, and MultipleValuesCustomField models. If it is owned and not one of those 3, then it should just * copy the OwnedModel nonRelation attributes. An example of that would be Address or Email * @param RedBeanModel $model * @param RedBeanModel $copyToModel - model to copy attribute values from $model to */ public static function copy(RedBeanModel $model, RedBeanModel $copyToModel) { $copyToModel->setIsCopied(); foreach ($model->attributeNames() as $attributeName) { if ($attributeName == 'owner') { continue; } $isReadOnly = $model->isAttributeReadOnly($attributeName); if (!$model->isRelation($attributeName) && !$isReadOnly) { static::copyNonRelation($model, $attributeName, $copyToModel); } elseif ($model->isRelation($attributeName) && !$isReadOnly && $model->isRelationTypeAHasOneVariant($attributeName)) { static::copyRelation($model, $attributeName, $copyToModel); } } if ($model instanceof OwnedSecurableItem) { static::copyRelation($model, 'owner', $copyToModel); } static::resolveExplicitPermissions($model, $copyToModel); }
/** * Copy the account from a related model to a model * @param RedBeanModel $model * @param RedBeanModel $relatedModel */ protected function addRelatedModelAccountToModel(RedBeanModel $model, RedBeanModel $relatedModel) { if (isset($relatedModel->account) && $relatedModel->account->id > 0 && in_array('account', $model->attributeNames())) { $model->account = $relatedModel->account; } }
/** * Process non derived relations assignment * @param RedBeanModel $primaryModel * @param RedBeanModel $selectedModel */ protected static function processNonDerivedRelationsAssignment($primaryModel, $selectedModel) { assert('$primaryModel instanceof RedBeanModel'); assert('$selectedModel instanceof RedBeanModel'); $modelClassName = get_class($primaryModel); foreach ($selectedModel->attributeNames() as $attribute) { if ($attribute == 'owner') { continue; } if ($modelClassName::isRelation($attribute) && !$modelClassName::isOwnedRelation($attribute) && !$primaryModel->isAttributeReadOnly($attribute)) { //Has one if ($modelClassName::isRelationTypeAHasOneVariant($attribute)) { $primaryModel->{$attribute} = $selectedModel->{$attribute}; } //Has many || Many many if (($modelClassName::isRelationTypeAHasManyVariant($attribute) || $modelClassName::isRelationTypeAManyManyVariant($attribute)) && $modelClassName::getRelationType($attribute) != RedBeanModel::HAS_MANY_BELONGS_TO) { foreach ($selectedModel->{$attribute} as $offset => $relatedModel) { if (!$primaryModel->{$attribute}->contains($relatedModel)) { $primaryModel->{$attribute}->add($relatedModel); } } } } } }
public static function getHasManyOpposingRelationName(RedBeanModel $model, $precedingModelClassName, $precedingRelation) { assert('is_string($precedingModelClassName)'); assert('is_string($precedingRelation)'); foreach ($model->attributeNames() as $attributeName) { if ($model->isRelation($attributeName) && ($model->getRelationType($attributeName) == RedBeanModel::HAS_ONE || $model->getRelationType($attributeName) == RedBeanModel::HAS_MANY_BELONGS_TO) && static::relationLinksToPrecedingRelation(get_class($model), $attributeName, $precedingModelClassName, $precedingRelation)) { return $attributeName; } } }