Duplicates a single belongs_to relation from one object to another.
Note: This will force a write on both parent / child objects.

@param DataObject $sourceObject
@param DataObject $destinationObject
@param string $relation
487989
protected function duplicateBelongsToRelation($sourceObject, $destinationObject, $relation)
    {
        // Check if original object exists
        $item = $sourceObject->getComponent($relation);
        if (!$item->isInDB()) {
            return;
        }

        $clonedItem = $item->duplicate(false);
        $destinationObject->setComponent($relation, $clonedItem);
        // After $clonedItem is assigned the appropriate FieldID / FieldClass, force write
        // @todo Write this component in onAfterWrite instead, assigning the FieldID then
        // https://github.com/silverstripe/silverstripe-framework/issues/7818
        $clonedItem->write();
    }
1080589
public function setRelation($relation, $relationColumn = null)
    {
        $this->relation = $relation;
        $this->relationColumn = $relationColumn;

        return $this;
    }
665539
public static function copy(string $source, string $destination)
  {
    if (is_dir($source)) {
      @mkdir($destination);

      $directory = dir($source);

      while (false !== ($readdirectory = $directory->read())) {
        if ($readdirectory == '.' || $readdirectory == '..') {
          continue;
        }

        $PathDir = $source . '/' . $readdirectory;

        if (is_dir($PathDir)) {
          Filesystem::copy($PathDir, $destination . '/' . $readdirectory);
          continue;
        }
        copy($PathDir, $destination . '/' . $readdirectory);
      }

      $directory->close();
    } else {
      copy($source, $destination);
    }
  }
1366601
protected function addForeignRelation(string $relation)
    {
        if (!in_array($relation, $this->foreignRelations)) {
            $this->foreignRelations[] = $relation;
        }
    }
1202608
public function createRelation($relationType, EntityRelationMeta $source, EntityRelationMeta $target, $side = NULL, $updateOtherSide = true) {
    Code::value($relationType, self::RELATION_MANY_TO_MANY, self::RELATION_MANY_TO_ONE, self::RELATION_ONE_TO_MANY, self::RELATION_ONE_TO_ONE);
    Code::value($side, self::SIDE_OWNING, self::SIDE_INVERSE, NULL);
    
    // Parameter übersetzen in constructor parameter von relation
    if ($side !== NULL) {
      $direction = EntityRelation::BIDIRECTIONAL;
      $whoIsOwningSide = $side === self::SIDE_OWNING ? EntityRelation::SOURCE : EntityRelation::TARGET;
    } else {
      $direction = EntityRelation::UNIDIRECTIONAL;
      $whoIsOwningSide = NULL;
    }
    
    $relation = new EntityRelation($source, $target, $relationType, $direction, $whoIsOwningSide);
    $this->buildRelation($relation);
    
    return $relation;
  }
154258
public function copyFrom($sourceModel, $destinationModel)
    {
        $this->guardProviderNames($sourceModel);
        $this->guardProviderNames($destinationModel);

        $setters = $this->getSetters();

        if (empty($setters) || !\is_array($setters)) {
            throw new DcGeneralRuntimeException(
                \sprintf(
                    'No relationship setter defined from %s to %s.',
                    $this->getSourceName(),
                    $this->getDestinationName()
                )
            );
        }

        foreach ($setters as $setter) {
            if (!$this->isValidSetter($setter)) {
                throw new DcGeneralRuntimeException(
                    \sprintf(
                        'Invalid relationship setter entry, ensure it is an array containing only "to_field" and
                    one of "from_field", "value": %s',
                        \var_export($setter, true)
                    )
                );
            }

            if (isset($setter['from_field'])) {
                $destinationModel->setProperty($setter['to_field'], $sourceModel->getProperty($setter['to_field']));

                continue;
            }

            $destinationModel->setProperty($setter['to_field'], $setter['value']);
        }
    }
900943
public function setRelation(Relationship $relation, $debug = false)
    {
        // Set the relation object.
        $this->relation = $relation;

        // Replace the attributes with those brought from the given relation.
        $this->attributes = $relation->getProperties();
        $this->setAttribute($this->primaryKey, $relation->getId());

        // Set the start and end nodes.
        $this->start = $relation->getStartNode();
        $this->end   = $relation->getEndNode();

        // Instantiate and fill out the related model.
        $relatedNode = ($this->isDirectionOut()) ? $this->end : $this->start;
        $attributes = array_merge(['id' => $relatedNode->getId()], $relatedNode->getProperties());

        // This is an existing relationship.
        $this->related = $this->related->newFromBuilder($attributes);
        $this->related->setConnection($this->related->getConnectionName());
    }
110281
public function copy( $source, $destination ) {

		$source = $this->get_file( $source );

		if ( false === $source ) {
			return false;
		}

		$destination_parent = $this->get_file( dirname( $destination ) );
		$filename = basename( $destination );

		if ( false === $destination_parent ) {
			return false;
		}

		$destination_parent->contents->$filename = clone $source;

		return true;
	}
941092
public function recurse_copy($source, $destination)
    {
        $directory = opendir($source);
        $this->fs->mkdir($destination, 0755);
        while (false !== ($file = readdir($directory))) {
            if (($file != '.') && ($file != '..')) {
                if (is_dir($source . '/' . $file)) {
                    $this->recurse_copy($source . '/' . $file, $destination . '/' . $file);
                } else {
                    $this->fs->copy($source . '/' . $file, $destination . '/' . $file);
                }
            }
        }
        closedir($directory);
    }
1298056
private function addChildSku(&$parentData, $relation)
    {
        if (isset($parentData['sku']) && !is_array($parentData['sku'])) {
            $parentData['sku'] = [$parentData['sku']];
        }

        $parentData['sku'][] = $relation['sku'];
        $parentData['sku'] = array_unique($parentData['sku']);
    }
404825
public static function copyRecursive($source, $destination)
    {
        if (!is_dir($destination)) {
            mkdir($destination);
        }

        $dir = opendir($source);

        if (!$dir) {
            throw new RuntimeException("Cannot copy '$source' to '$destination'");
        }

        while (false !== ($file = readdir($dir))) {
            if ($file != '.' && $file != '..') {
                if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
                    static::copyRecursive(
                        $source . DIRECTORY_SEPARATOR . $file,
                        $destination . DIRECTORY_SEPARATOR . $file
                    );
                } else {
                    copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
                }
            }
        }
        closedir($dir);
    }
1072596
protected function getRelatedType(ObjectId $sourceOid, $role) {
    $persistenceFacade = $this->getPersistenceFacade();
    $sourceMapper = $persistenceFacade->getMapper($sourceOid->getType());
    $relation = $sourceMapper->getRelation($role);
    return $relation->getOtherType();
  }
345809
protected function populateObject($sourceObject, $targetObject, array $targetObjectCtorArgs = array()) {
		if (is_object($targetObject)) {
			$resultObject = $targetObject;
		}
		else {
			$ref = new ReflectionClass($targetObject);
			$resultObject = $ref->newInstanceArgs($targetObjectCtorArgs);
		}

		$ref = new ReflectionClass($sourceObject);
		foreach ($ref->getProperties() as $property) {
			$name=$property->getName();
			$resultObject->$name = $sourceObject->$name;
		}

		return $resultObject;
	}
321932
protected function synchronize(string $relation)
    {
        $actualContent = $this->relationships[$relation];

        $relationshipObject = $this->entityMap->$relation($this->getEntityObject());
        $relationshipObject->setParent($this->wrappedEntity);
        $relationshipObject->sync($actualContent);
    }
911862
public function addRelationshipObject($key, RelationshipObject $relationshipObject, array $options=[]) {
		if ($relationshipObject->hasResource($this)) {
			throw new DuplicateException('can not add relation to self');
		}
		
		if ($this->relationships === null) {
			$this->setRelationshipsObject(new RelationshipsObject());
		}
		
		$this->validator->claimUsedFields([$key], Validator::OBJECT_CONTAINER_RELATIONSHIPS, $options);
		
		$this->relationships->addRelationshipObject($key, $relationshipObject);
	}
858682
public function duplicate($model, $relation = null) {
		$clone = $this->cloneModel($model);
		$this->duplicateAttachments($clone);
		$this->saveClone($clone, $relation, $model);
		$this->cloneRelations($model, $clone);
		return $clone;
	}
1315654
private void copyRelations(CmsDbContext dbc, CmsResource source, CmsResource target) throws CmsException {

        // copy relations all relations
        CmsObject cms = new CmsObject(getSecurityManager(), dbc.getRequestContext());
        Iterator<CmsRelation> itRelations = getRelationsForResource(
            dbc,
            source,
            CmsRelationFilter.TARGETS.filterNotDefinedInContent()).iterator();
        while (itRelations.hasNext()) {
            CmsRelation relation = itRelations.next();
            if (relation.getType().getCopyBehavior() == CopyBehavior.copy) {
                try {
                    CmsResource relTarget = relation.getTarget(cms, CmsResourceFilter.ALL);
                    addRelationToResource(dbc, target, relTarget, relation.getType(), true);
                } catch (CmsVfsResourceNotFoundException e) {
                    // ignore this broken relation
                    if (LOG.isWarnEnabled()) {
                        LOG.warn(e.getLocalizedMessage(), e);
                    }
                }
            }
        }
        // repair categories
        repairCategories(dbc, getProjectIdForContext(dbc), target);
    }