Set all has_one relationships to an ElementalArea to a valid ID if they're unset

@param array $elementalAreaRelations indexed array of relationship names that are to ElementalAreas
@return DataObject
15350
public function ensureElementalAreasExist($elementalAreaRelations)
    {
        foreach ($elementalAreaRelations as $eaRelationship) {
            $areaID = $eaRelationship . 'ID';

            if (!$this->owner->$areaID) {
                $area = ElementalArea::create();
                $area->OwnerClassName = get_class($this->owner);
                $area->write();
                $this->owner->$areaID = $area->ID;
            }
        }
        return $this->owner;
    }
1306583
protected function prepareUpdate(array $updates, $oneToOneRelation = false)
    {
        $prepared = [];

        foreach ($this->builder->fields() as $field) {
            $columns = $field->column();

            // If column not in input array data, then continue.
            if (!array_has($updates, $columns)) {
                continue;
            }

            if ($this->invalidColumn($columns, $oneToOneRelation)) {
                continue;
            }

            $value = $this->getDataByColumn($updates, $columns);

            $value = $field->prepare($value);

            if (is_array($columns)) {
                foreach ($columns as $name => $column) {
                    array_set($prepared, $column, $value[$name]);
                }
            } elseif (is_string($columns)) {
                array_set($prepared, $columns, $value);
            }
        }

        return $prepared;
    }
1004775
private function setSelectedRelationshipsTo(
        string $sectionHandle,
        string $fieldHandle,
        array $fieldInfo,
        int $id
    ): array {
        /** @var CommonSectionInterface $editing */
        $editing = $this->readSection->read(
            ReadOptions::fromArray([
                ReadOptions::SECTION => $sectionHandle,
                ReadOptions::ID => (int)$id
            ])
        )->current();

        try {
            $method = $this->handleToPropertyName($fieldHandle, $editing::FIELDS);
            $relationshipsEntityMethod = 'get' . ucfirst($method);
            $related = $editing->{$relationshipsEntityMethod}();
            if (!is_null($related)) {
                $relatedIds = [];
                if (is_iterable($related)) {
                    /** @var CommonSectionInterface $relation */
                    foreach ($related as $relation) {
                        $relatedIds[] = $relation->getId();
                    }
                } else {
                    $relatedIds[] = $related->getId();
                }
                foreach ($fieldInfo[$fieldHandle][$fieldInfo[$fieldHandle]['to']] as &$relatable) {
                    if (!empty($relatable['id']) && in_array($relatable['id'], $relatedIds)) {
                        $relatable['selected'] = true;
                    }
                }
            }
        } catch (\Exception $exception) {
            // Empty because we can just return the fieldInfo the way it was if the above fails
        }

        return $fieldInfo;
    }
1195619
public function setDealerAdmin(ChildDealerAdmin $v = null)
    {
        if ($v === null) {
            $this->setId(NULL);
        } else {
            $this->setId($v->getId());
        }

        $this->aDealerAdmin = $v;

        // Add binding for other direction of this n:n relationship.
        // If this object has already been added to the ChildDealerAdmin object, it will not be re-added.
        if ($v !== null) {
            $v->addDealerAdminVersion($this);
        }


        return $this;
    }
285723
public function setMany(array $data, $lifetime = 0, $tags = null) {
		if ($tags === null) {
			return $this->getBackend()->setMany($data, $lifetime);
		}
		
		$tagBackend = $this->getTagHolderBackend();
		
		if (is_array($tags)) {
			$tags      = array_map([$this, 'prepareTag'], array_values($tags));
			$tagsData  = $tagBackend->getMany($tags);
			$tagsCount = count($tagsData, COUNT_RECURSIVE);
			
			if (empty($tagsData)) {
				$tagsData = array_fill_keys($tags, array_keys($data));
			} else {
				foreach ($tags as $tag) {
					if (empty($tagsData[$tag])) {
						$tagsData[$tag] = array_keys($data);
					} else {
						$tagsData[$tag] = array_merge(
							array_diff(array_keys($data), $tagsData[$tag]),
							$tagsData[$tag]
						);
					}
				}
			}
			
			if (count($tagsData, COUNT_RECURSIVE) !== $tagsCount) {
				$tagBackend->setMany($tagsData, $lifetime);
			}
		} else {
			$tag      = $this->prepareTag($tags);
			$tagData  = $tagBackend->get($tag);
			$tagCount = count($tagData);
			
			if (empty($tagData)) {
				$tagData = array_keys($data);
			} else {
				$tagData = array_merge(
					array_diff(array_keys($data), $tagData),
					$tagData
				);
			}
			
			if (count($tagData) !== $tagCount) {
				$tagBackend->set($tag, $tagData, 0);
			}
		}
		
		return $this->getBackend()->setMany($data, $lifetime);
	}
525379
protected function prepareUpdate(array $updates, $oneToOneRelation = false)
    {
        $prepared = [];

        /** @var Field $field */
        foreach ($this->builder->fields() as $field) {
            $columns = $field->column();

            // If column not in input array data, then continue.
            if (!Arr::has($updates, $columns)) {
                continue;
            }

            if ($this->invalidColumn($columns, $oneToOneRelation)) {
                continue;
            }

            $value = $this->getDataByColumn($updates, $columns);

            $value = $field->prepare($value);

            if (is_array($columns)) {
                foreach ($columns as $name => $column) {
                    Arr::set($prepared, $column, $value[$name]);
                }
            } elseif (is_string($columns)) {
                Arr::set($prepared, $columns, $value);
            }
        }

        return $prepared;
    }
63753
public function attach($ids, array $attributes = [], $touch = true)
    {
        list($idsOnly, $idsAttributes) = $this->getIdsWithAttributes($ids, $attributes);

        $this->parent->fireModelEvent('pivotAttaching', true, $this->getRelationName(), $idsOnly, $idsAttributes);
        $parentResult = parent::attach($ids, $attributes, $touch);
        $this->parent->fireModelEvent('pivotAttached', false, $this->getRelationName(), $idsOnly, $idsAttributes);

        return $parentResult;
    }
281520
public function formatMappingsWithRelations($mappings, $associationMappings)
    {
        $formattedMappings = $this->formatMappings($mappings);

        return $this->formatRelationMappings($formattedMappings, $associationMappings);
    }
343864
public function setData(array $data): self
    {
        list($this->foreignKeys, $this->belongsToMany, $this->relationshipTypes,
            $this->reversedRelationships,$this->tableNames, $this->primaryKeys,
            $this->attributeTypes, $this->attributeLengths, $this->attributes, $this->rawAttributes,
            $this->reversedClasses) = $data;

        return $this;
    }
272733
public function getOwnedAreaRelationName()
    {
        $has_one = $this->config()->get('has_one');

        foreach ($has_one as $relationName => $relationClass) {
            if ($relationClass === ElementalArea::class && $relationName !== 'Parent') {
                return $relationName;
            }
        }

        return 'Elements';
    }
926408
public function havingRelations(array $relations)
    {
        $instance = $this->havingStrategyInstance();

        $instance->havingStrategy()->havingRelations($relations);

        return $instance;
    }
127588
public function addDataAttributes(array $dataAttributes)
    {
        foreach ($dataAttributes as $name => $value) {
            $this->addDataAttribute($name, $value);
        }
        return $this;
    }
1142261
public function setChannelElementsFromArray(array $elementArray)
    {
        foreach ($elementArray as $elementName => $content) {
            $this->setChannelElement($elementName, $content);
        }

        return $this;
    }
678474
public function fromFullyQualifiedRelationships(array $relationshipNames)
    {
        foreach ($relationshipNames as $relationshipName) {
            $relationship = $this->getRelationshipResourceForReference($relationshipName);

            if ($relationship !== null) {
                return $relationship;
            }
        }

        return null;
    }
218959
public function getAreaRelationName()
    {
        // Allow repeated calls to return from internal cache
        if (isset($this->cacheData['area_relation_name'])) {
            return $this->cacheData['area_relation_name'];
        }

        $page = $this->getPage();

        $result = 'ElementalArea';

        if ($page) {
            $has_one = $page->config()->get('has_one');
            $area = $this->Parent();

            foreach ($has_one as $relationName => $relationClass) {
                if ($page instanceof BaseElement && $relationName === 'Parent') {
                    continue;
                }
                if ($relationClass === $area->ClassName && $page->{$relationName}()->ID === $area->ID) {
                    $result = $relationName;
                    break;
                }
            }
        }

        $this->setAreaRelationNameCache($result);

        return $result;
    }
665736
public static function setResponseDataRelationshipSelfLinks($propertyName, array &$mappings, array &$parent)
    {
        $data = [];
        $parentType = $parent[Serializer::CLASS_IDENTIFIER_KEY];

        if (\is_scalar($parentType) && !empty($mappings[$parentType])) {
            $copy = $parent;
            RecursiveFormatterHelper::formatScalarValues($copy);
            if (!empty($copy[Serializer::CLASS_IDENTIFIER_KEY])) {
                unset($copy[Serializer::CLASS_IDENTIFIER_KEY]);
            }

            $idProperties = array_keys($copy);
            foreach ($idProperties as &$property) {
                $property = sprintf('{%s}', $property);
            }
            $idValues = array_values($copy);

            if (!empty($mappings[$parentType]) && !empty($selfLink = $mappings[$parentType]->getRelationshipSelfUrl($propertyName))) {
                $url = self::buildUrl($mappings, $idProperties, $idValues, $selfLink, $parentType);

                if ($url !== $selfLink) {
                    $data[JsonApiTransformer::SELF_LINK][JsonApiTransformer::LINKS_HREF] = $url;
                }
            }
        }

        return $data;
    }
148757
protected function mapModelDataToElementCollection(ElementCollection $children)
    {
        foreach ($children->all() as $child) {
            $this->mapModelDataToElement($child);
        }
    }