@param $db_type
@param GeneratorFieldRelation|null $relation

@return string
179657
private function getPHPDocType($db_type, $relation = null)
    {
        switch ($db_type) {
            case 'datetime':
                return 'string|\Carbon\Carbon';
            case 'text':
                return 'string';
            case '1t1':
            case 'mt1':
                return '\\'.$this->commandData->config->nsModel.'\\'.$relation->inputs[0].' '.camel_case($relation->inputs[0]);
            case '1tm':
                return '\Illuminate\Database\Eloquent\Collection'.' '.$relation->inputs[0];
            case 'mtm':
            case 'hmt':
                return '\Illuminate\Database\Eloquent\Collection'.' '.camel_case($relation->inputs[1]);
            default:
                return $db_type;
        }
    }
412190
public function setListParent($relation, $recordKey = null)
    {
        if (false !== $relation && empty($relation)) {
            return $this->clearListParent();
        }

        if (false === $relation) {
            session()->put($this->getSessionKey(static::TYPE_PARENT), static::PARENT_DISABLE);
        } else {
            session()->put($this->getSessionKey(static::TYPE_PARENT), $relation . ':' . $recordKey);
        }

        return $this;
    }
995808
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
    {
        $instance = $this->newRelatedInstance($related);

        // Here we will gather up the morph type and ID for the relationship so that we
        // can properly query the intermediate table of a relation. Finally, we will
        // get the table and create the relationship instances for the developers.
        [$type, $id] = $this->getMorphs($name, $type, $id);

        $table = $instance->getTable();

        $localKey = $localKey ?: $this->getKeyName();

        return $this->newMorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
    }
501520
private static function _loadDatabaseDefinition()
	{
		$db = self::getAdapter();
	
		//Get table columns
		$dbData = array();
		$tables = $db->query("SHOW TABLES")->execute();
		foreach($tables as $tableListRow)
		{
			$table = current($tableListRow);
			$columns = $db->query("DESCRIBE {$table}")->execute();
			$columnData = array();
			foreach($columns as $columnListRow)
			{
				$columnData[$columnListRow['Field']] = $columnListRow['Type'];
			}
			$dbData['tables'][$table]['columns'] = $columnData;
		}
		
		//Get relationships
		$schema = $db->getDriver()->getConnection()->getCurrentSchema();
		$relations = $db->query("
			SELECT table_name, column_name, referenced_table_name, referenced_column_name
			FROM information_schema.key_column_usage
			WHERE table_schema='{$schema}' AND referenced_table_schema IS NOT NULL;
		")->execute();
		$relationData = array();
		foreach($relations as $relation)
		{
			$table = $relation['table_name'];
			$column = $relation['column_name'];
			$otherTable = $relation['referenced_table_name'];
			
			$dbData['tables'][$table]['owners'][$column] = $otherTable;
			$dbData['tables'][$otherTable]['children'][$table][] = $column;

			$table = $relation['table_name'];
			unset($relation['table_name']);
			$relationData[$table][$relation['column_name']] = $relation['referenced_table_name'];
			
		}
		$dbData['relations'] = $relationData;
		
		self::$_databaseDef = $dbData;
	}
465309
public function getRelationFieldName() {
        if ($relationFieldId = $this->getRelationFieldId()) {
            $names = $this->getRespondentTrack()->getTrackEngine()->getFieldNames();
            $fieldPrefix = \Gems\Tracker\Model\FieldMaintenanceModel::FIELDS_NAME . \Gems\Tracker\Engine\FieldsDefinition::FIELD_KEY_SEPARATOR;
            $key = $fieldPrefix . $relationFieldId;

            return array_key_exists($key, $names) ? lcfirst($names[$key]) : null;
        }

        return null;
    }
448239
protected function getClassToProxy($relation, array $attributes)
    {
        if ($this->entityMap->isPolymorphic($relation)) {
            $localTypeAttribute = $this->entityMap->getLocalKeys($relation)['type'];

            return $attributes[$localTypeAttribute];
        }

        return $this->entityMap->getTargettedClass($relation);
    }
775793
protected static function getHasManyRelationField($model, $relation, $activeRelation)
    {
        $widgetOptions = self::getRelationWidgetOptions($model, $relation, $activeRelation, true);
        return static::getRelationWidget($model, $relation, $activeRelation, $widgetOptions);
    }
340023
public function join(
        $foreign_table,
        $master_field = null,
        $join_kind = null,
        $_foreign_alias = null,
        $relation = null
    ) {
        if (!$_foreign_alias) {
            $_foreign_alias = '_'.$foreign_table[0];
        }
        $_foreign_alias = $this->_unique($this->relations, $_foreign_alias);

        /** @type SQL_Relation $rel */
        $rel = $this->add('SQL_Relation', $_foreign_alias);

        return $this->relations[$_foreign_alias] = $rel
            ->set($foreign_table, $master_field, $join_kind, $relation);
    }
390533
private function isRelationForeignKeyNullable(Relation $relation)
    {
        $reflectionObj = new \ReflectionObject($relation);
        if (!$reflectionObj->hasProperty('foreignKey')) {
            return false;
        }
        $fkProp = $reflectionObj->getProperty('foreignKey');
        $fkProp->setAccessible(true);

        return isset($this->nullableColumns[$fkProp->getValue($relation)]);
    }
522531
public function getLink($relation)
    {
        $links = $this->getAllLinks();
        foreach ($links as $link) {
            if ($link->getRelation() === $relation) {
                return $link;
            }
        }

        return null;
    }
209373
private function checkRelationInformation()
    {
        if ($this->getInspector()->getSchemaOid($this->schema) === null) {
            throw new GeneratorException(sprintf("Schema '%s' not found.", $this->schema));
        }

        $table_oid = $this->getInspector()->getTableOid($this->schema, $this->relation);

        if ($table_oid === null) {
            throw new GeneratorException(
                sprintf(
                    "Relation '%s' could not be found in schema '%s'.",
                    $this->relation,
                    $this->schema
                )
            );
        }

        return $table_oid;
    }
298330
RelationBuilder getRelationBuilder(Object target, Relation relation, Node source)
    {
        RelationBuilder relationBuilder = new RelationBuilder(target, relation, source);
        relationBuilder.assignGraphGenerator(this.generator);
        return relationBuilder;
    }
1009301
public function searchDatabase(
        Connection $connection,
        string $databaseName,
        string $searchTerm
    ): \Generator {
        $databaseMetadata = (new MetadataFactory())->getDatabaseMetadata($connection, $databaseName);

        foreach ($databaseMetadata->getAllTableMetadata() as $tableMetadata) {
            $resultList = $this->searchTable(
                $connection,
                $tableMetadata->getDatabaseName(),
                $tableMetadata->getTableName(),
                $searchTerm
            );

            foreach ($resultList as $result) {
                yield $result;
            }
        }
    }
929226
protected function getJoinClause(Model $parent, Relation $relation, $table, $type)
    {
        list($fk, $pk) = $this->getJoinKeys($relation);

        $join = (new Join($this->query, $type, $table))->on($fk, '=', $pk);

        if (in_array(SoftDeletes::class, class_uses_recursive($relation->getRelated()))) {
            $join->whereNull($relation->getRelated()->getQualifiedDeletedAtColumn());
        }

        if ($relation instanceof MorphOneOrMany) {
            $join->where($relation->getQualifiedMorphType(), '=', $parent->getMorphClass());
        } elseif ($relation instanceof MorphToMany || $relation instanceof MorphByMany) {
            $join->where($relation->getMorphType(), '=', $parent->getMorphClass());
        }

        return $join;
    }
258151
public function getDbFieldType() : string
    {
        $dbFieldType = $this->config->getProvider('dbFieldType');
        $dbFieldType = new $dbFieldType($this->config);
        $dbFieldType = $dbFieldType->provide();

        return $dbFieldType;
    }
804301
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
    {
        $localKey = $this->localKey;
        $range    = [];

        foreach ($resultSet as $result) {
            // 获取关联外键列表
            if (isset($result->$localKey)) {
                $range[] = $result->$localKey;
            }
        }

        if (!empty($range)) {
            $where = [
                [$this->foreignKey, 'in', $range],
            ];
            $data = $this->eagerlyOneToMany($where, $relation, $subRelation, $closure);

            // 关联属性名
            $attr = Db::parseName($relation);

            // 关联数据封装
            foreach ($resultSet as $result) {
                $pk = $result->$localKey;
                if (!isset($data[$pk])) {
                    $data[$pk] = [];
                }

                foreach ($data[$pk] as &$relationModel) {
                    $relationModel->setParent(clone $result);
                }

                $result->setRelation($attr, $this->resultSetBuild($data[$pk]));
            }
        }
    }
1059052
public function loadRelations($contentId, $contentVersionNo = null, $relationType = null)
    {
        try {
            return $this->innerGateway->loadRelations($contentId, $contentVersionNo, $relationType);
        } catch (DBALException $e) {
            throw new RuntimeException('Database error', 0, $e);
        } catch (PDOException $e) {
            throw new RuntimeException('Database error', 0, $e);
        }
    }