Get the relationship name of the has one/many

@return string
545494
protected function getHasOneOrManyCaller()
    {
        $self = __FUNCTION__;

        $caller = Arr::first(debug_backtrace(false), function ($key, $trace) use ($self) {
            $caller = $trace['function'];

            return ! in_array($caller, CmsModel::$hasRelationMethods) && $caller != $self;
        });

        return ! is_null($caller) ? $caller['function'] : null;
    }
92328
public function getAlias() {
        //Check and see if there is another relationship with the same name
        $multipleFound = false;
        foreach($this->getRelationships() as $relationship) {
            if($relationship !== $this && $relationship->getRemoteTable() == $this->getRemoteTable()) {
                $multipleFound = true;
            }
        }
        $returnVar = '';
        if($multipleFound) {
            if($this->isPlural()) {
                $name = $this->getRemoteColumn();
                //Strip out local table
                if(stripos($name, $this->getRemoteTable()) === 0) {
                    $name = substr($name, strlen($this->getRemoteTable()));
                }
                //Strip out remote table name, with id
                if(stripos(substr($name, strlen($this->getLocalTable() . 'Id') * -1), $this->getLocalTable() . 'Id') === 0) {
                    $name = substr($name, 0, strlen($this->getLocalTable() . 'Id') * -1);
                }
            } else {
                $name = $this->getLocalColumn();
                //Strip out local table
                if(stripos($name, $this->getLocalTable()) === 0) {
                    $name = substr($name, strlen($this->getLocalTable()));
                }
                //Strip out remote table name, with id
                if(stripos(substr($name, strlen($this->getRemoteTable() . 'Id') * -1), $this->getRemoteTable() . 'Id') === 0) {
                    $name = substr($name, 0, strlen($this->getRemoteTable() . 'Id') * -1);
                }
            }
            $returnVar .= $name;
        }
        $returnVar .= $this->getRemoteShortModel();
        if($this->isPlural()) {
            $pluralHelper = new PluralHelper();
            $returnVar = $pluralHelper->pluralize($returnVar);
        }
        return ucfirst($returnVar);
    }
815736
private function _associations_names()
    {
        $associations = array();
        foreach ($this->associations() as $assocs) {
            foreach ($assocs as $k => $v)
                $associations[] = is_int($k) ? $v : $k;
        }
        return $associations;
    }
230014
public function getRelatedIds()
    {
        $fullKey = $this->relatedMap->getQualifiedKeyName();

        return $this->getQuery()->select($fullKey)->lists($this->relatedMap->getKeyName());
    }
48638
public function getModelName()
    {
        if (!$name = $this->modelName) {
            $tableName = $this->getName();

            $modelNames = [];
            foreach (explode('_', $tableName) as $word) {
                $modelNames[] = ucfirst(Inflector::singularize($word));
            } //foreach
            $modelName = implode('', $modelNames);

            $this->setModelName($name = ucfirst($this->isReservedPHPWord($modelName) ? $tableName : $modelName));
        } // if

        return $name;
    }
369428
protected function _hasConflictingName() {
        $relation       = $this->getRelation();
        $model          = $this->getModel();
        // $remoteModelId   = $this->_getRemoteModelId();
        $nameConflicts  = $model->id === $relation->name;

        return $nameConflicts;
    }
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;
    }
709486
private function buildRelationshipsList() {
        $content = '		return array(' . "\n";
        $added = false;
        foreach($this->getRelationships() as $relationship) {
            if(!$relationship->isPlural()) {
                $added = true;
                $content .= '			\'' . $relationship->getAlias() . '\',' . "\n";
            }
        }
        if($added) {
            $content = substr($content, 0, -2) . "\n"; //Remove trailing comma
        }
        $content .= '		);';
        $method = new Object\Method();
        $method->setAccess('public');
        $method->setDescription('Returns a list of all parent relationships, these will all return a Base Model instance');
        $method->setName('getParentRelationships');
        $method->setReturnType('string[] values are the alias of the relationship');
        $method->setContent($content);
        $this->getAbstractClass()->addMethod($method);

        $content = '		return array(' . "\n";
        $added = false;
        foreach($this->getRelationships() as $relationship) {
            if($relationship->isPlural()) {
                $added = true;
                $content .= '			\'' . $relationship->getAlias() . '\',' . "\n";
            }
        }
        if($added) {
            $content = substr($content, 0, -2) . "\n"; //Remove trailing comma
        }
        $content .= '		);';
        $method = new Object\Method();
        $method->setAccess('public');
        $method->setDescription('Returns a list of all children relationships, these will all return a ResultSet');
        $method->setName('getChildrenRelationships');
        $method->setReturnType('string[] values are the alias of the relationship');
        $method->setContent($content);
        $this->getAbstractClass()->addMethod($method);

    }
303412
protected function getRelationName($recordField)
    {
        $relationName = null;
        if (isset($this->relationCallbacks[$recordField])) {
            $relationName = $this->relationCallbacks[$recordField]['relationname'];
        }
        if (strpos($recordField, '.') !== false) {
            list($relationName, $columnName) = explode('.', $recordField);
        }

        return $relationName;
    }
848538
public function getForeignKey()
    {
        if ($this->foreignKey !== null) {
            return $this->foreignKey;
        }
        $this->init();
        switch ($this->type) {
            case self::HAS_MANY:
            case self::HAS_ONE:
                return $this->owner->getTableName() . '_' . $this->owner->getPrimaryKey();
            case self::BELONGS_TO:
                return $this->foreign->getPrimaryKey();
            default:
                throw new \Exception(sprintf("Relation type '%s' is not supported.", $this->type));
        }
    }
1312940
public function getDirtyRelations()
    {
        $dirtyRelations = [];
        foreach ($this->_relations as $relationName) {
            if (array_key_exists($relationName, $this->_oldRelationValue)) {
                $dirtyRelations[$relationName] = $this->owner->{$relationName};
            }
        }
        return $dirtyRelations;
    }
978108
public function type() {
		if($this->get('relation_type'))
			return $this->get('relation_type');
		elseif($this->get('poymorphic'))
			throw new \Exception('Parameter relation_type must be provided for polymorphic relations.');

		$rev = $this->reverse();

		if($this->get('many')) {
			if($rev->get('many'))
				return 'HMABT';
			else
				return 'hasMany';
		}
		else {
			if($rev->get('many'))
				return 'belongsTo';
			else
				return 'hasOne';
		}
	}
985346
public function getAssociated()
    {
        $associated = [];
        foreach (['hasMany', 'hasOne', 'belongsTo'] as $association) {
            foreach ($this->{$association} as $name => $options) {
                $associated[$name] = $association;
            }
        }
        return $associated;
    }
290090
public function getReferenceName()
    {
        $options = $this->getOptions();
        if (!empty($options['tag'])) {
            return 'Tag: ' . $options['tag'];
        } elseif (!empty($options['branch'])) {
            return 'Branch: ' . $options['branch'];
        } elseif (!empty($options['sha1'])) {
            return 'Sha1: ' . $options['sha1'];
        }
        return '';
    }
506815
protected function getRelationsConfigReplace()
    {
        $relationships = $this->collectRelationshipDataForConfig();

        if ( ! count($relationships)) return '';


        $replace = $this->tab() . "protected \$relationsConfig = [\n";

        foreach ($relationships as $name => $relationship) {

            $replace .= $this->tab(2) . "'" . $name . "' => [\n";

            $rows = [];

            if (array_get($relationship, 'field')) {
                $rows['field'] = $relationship['field'];
            }

            if (array_get($relationship, 'special')) {
                $rows['type'] = $relationship['specialString'];
            } else {
                $rows['parent'] = ($relationship['reverse'] ? 'false' : 'true');
            }

            if (array_get($relationship, 'translated') === true) {
                $rows['translated'] = 'true';
            }

            $longestPropertyLength = $this->getLongestKey($rows);

            foreach ($rows as $property => $value) {

                $replace .= $this->tab(3) . "'"
                    . str_pad($property . "'", $longestPropertyLength + 1)
                    . " => {$value},\n";
            }


            $replace .= $this->tab(2) . "],\n";
        }

        $replace .= $this->tab() . "];\n\n";

        return $replace;
    }
1303758
public function getContent()
    {
        return strtr($this->getRelationStubContent(), [
            '{{name}}'         => $this->name,
            '{{relatedTable}}' => $this->relatedModel->getTable(),
            '{{relatedModel}}' => $this->relatedModel->getClass(),
            '{{foreignKey}}'   => $this->foreignKey,
        ]);
    }
1346636
private function getRelationships()
    {
        $relationships = [];

        // Loop through input, and check whether any key is a valid relationship
        foreach ($this->dataset as $model => $value)
        {
            // Skip anything which isn't a valid relationship
            if ( ! $this->isRelationship($model, $this->conditions)) continue;

            // Capture relationship and it's type
            $relationships[$model] = $this->getRelationshipType(Str::camel($model));
        }

        return $relationships;
    }