protected function makeRelations(array $models)
    {
        foreach ($models as $model) {
            $localTable  = $model->getTable();
            $foreignKeys = $localTable->getTableForeignKeys();

            foreach ($foreignKeys as $foreignKey) {
                if (!isset($models[$foreignKey->getForeignTableName()])) {
                    continue;
                }

                $foreignModel = $models[$foreignKey->getForeignTableName()];
                $foreignTable = $foreignModel->getTable();

                $localColumns   = $foreignKey->getLocalColumns();
                $foreignColumns = $foreignKey->getForeignColumns();

                $localIndex   = $localTable->getTableIndexByColumnsName($localColumns);
                $foreignIndex = $foreignTable->getTableIndexByColumnsName($foreignColumns);

                $localTableClass   = str_singular($model->getFullClass());
                $foreignTableClass = str_singular($foreignModel->getFullClass());

                $localColumn   = reset($localColumns);
                $foreignColumn = reset($foreignColumns);

                // Guess foreign method name
                $localMethodName   = str_replace(['_id'], '', snake_case($localColumn));
                $foreignMethodName = $model->getClass();

                // Try to find out connection type
                if ($localIndex !== false && $foreignIndex !== false && $localIndex->isUnique() && $foreignIndex->isUnique()) {
                    // One To One
                    $localTable->addHasOneRelation($foreignTableClass, $localMethodName, $foreignColumn, $localColumn);
                    $foreignTable->addBelongsToRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                } elseif ($foreignIndex !== false && $foreignIndex->isUnique()) {
                    // Many To One
                    $localTable->addBelongsToRelation($foreignTableClass, $localMethodName, $localColumn, $foreignColumn);
                    $foreignTable->addHasManyRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                } elseif ($localIndex !== false && $localIndex->isUnique()) {
                    // One To Many
                    $localTable->addHasManyRelation($foreignTableClass, $localMethodName, $foreignColumn, $localColumn);
                    $foreignTable->addBelongsToRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                } else {
                    // Many To Many without pivot table
                    $localTable->addHasManyRelation($foreignTableClass, $localMethodName, $foreignColumn, $localColumn);
                    $foreignTable->addHasManyRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                }
            }
        }
    }
protected function makeRelations(array $models)
    {
        foreach ($models as $model) {
            $localTable  = $model->getTable();
            $foreignKeys = $localTable->getTableForeignKeys();

            foreach ($foreignKeys as $foreignKey) {
                if (!isset($models[$foreignKey->getForeignTableName()])) {
                    continue;
                }

                $foreignModel = $models[$foreignKey->getForeignTableName()];
                $foreignTable = $foreignModel->getTable();

                $localColumns   = $foreignKey->getLocalColumns();
                $foreignColumns = $foreignKey->getForeignColumns();

                $localIndex   = $localTable->getTableIndexByColumnsName($localColumns);
                $foreignIndex = $foreignTable->getTableIndexByColumnsName($foreignColumns);

                $localTableClass   = str_singular($model->getFullClass());
                $foreignTableClass = str_singular($foreignModel->getFullClass());

                $localColumn   = reset($localColumns);
                $foreignColumn = reset($foreignColumns);

                // Guess foreign method name
                $localMethodName   = str_replace(['_id'], '', snake_case($localColumn));
                $foreignMethodName = $model->getClass();

                // Try to find out connection type
                if ($localIndex !== false && $foreignIndex !== false && $localIndex->isUnique() && $foreignIndex->isUnique()) {
                    // One To One
                    $localTable->addHasOneRelation($foreignTableClass, $localMethodName, $foreignColumn, $localColumn);
                    $foreignTable->addBelongsToRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                } elseif ($foreignIndex !== false && $foreignIndex->isUnique()) {
                    // Many To One
                    $localTable->addBelongsToRelation($foreignTableClass, $localMethodName, $localColumn, $foreignColumn);
                    $foreignTable->addHasManyRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                } elseif ($localIndex !== false && $localIndex->isUnique()) {
                    // One To Many
                    $localTable->addHasManyRelation($foreignTableClass, $localMethodName, $foreignColumn, $localColumn);
                    $foreignTable->addBelongsToRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                } else {
                    // Many To Many without pivot table
                    $localTable->addHasManyRelation($foreignTableClass, $localMethodName, $foreignColumn, $localColumn);
                    $foreignTable->addHasManyRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                }
            }
        }
    }
protected function makeRelations ( array $ models ) { foreach ( $ models as $ model ) { $ localTable = $ model -> getTable ( ) ; $ foreignKeys = $ localTable -> getTableForeignKeys ( ) ; foreach ( $ foreignKeys as $ foreignKey ) { if ( ! isset ( $ models [ $ foreignKey -> getForeignTableName ( ) ] ) ) { continue ; } $ foreignModel = $ models [ $ foreignKey -> getForeignTableName ( ) ] ; $ foreignTable = $ foreignModel -> getTable ( ) ; $ localColumns = $ foreignKey -> getLocalColumns ( ) ; $ foreignColumns = $ foreignKey -> getForeignColumns ( ) ; $ localIndex = $ localTable -> getTableIndexByColumnsName ( $ localColumns ) ; $ foreignIndex = $ foreignTable -> getTableIndexByColumnsName ( $ foreignColumns ) ; $ localTableClass = str_singular ( $ model -> getFullClass ( ) ) ; $ foreignTableClass = str_singular ( $ foreignModel -> getFullClass ( ) ) ; $ localColumn = reset ( $ localColumns ) ; $ foreignColumn = reset ( $ foreignColumns ) ; // Guess foreign method name $ localMethodName = str_replace ( [ '_id' ] , '' , snake_case ( $ localColumn ) ) ; $ foreignMethodName = $ model -> getClass ( ) ; // Try to find out connection type if ( $ localIndex !== false && $ foreignIndex !== false && $ localIndex -> isUnique ( ) && $ foreignIndex -> isUnique ( ) ) { // One To One $ localTable -> addHasOneRelation ( $ foreignTableClass , $ localMethodName , $ foreignColumn , $ localColumn ) ; $ foreignTable -> addBelongsToRelation ( $ localTableClass , $ foreignMethodName , $ localColumn , $ foreignColumn ) ; } elseif ( $ foreignIndex !== false && $ foreignIndex -> isUnique ( ) ) { // Many To One $ localTable -> addBelongsToRelation ( $ foreignTableClass , $ localMethodName , $ localColumn , $ foreignColumn ) ; $ foreignTable -> addHasManyRelation ( $ localTableClass , $ foreignMethodName , $ localColumn , $ foreignColumn ) ; } elseif ( $ localIndex !== false && $ localIndex -> isUnique ( ) ) { // One To Many $ localTable -> addHasManyRelation ( $ foreignTableClass , $ localMethodName , $ foreignColumn , $ localColumn ) ; $ foreignTable -> addBelongsToRelation ( $ localTableClass , $ foreignMethodName , $ localColumn , $ foreignColumn ) ; } else { // Many To Many without pivot table $ localTable -> addHasManyRelation ( $ foreignTableClass , $ localMethodName , $ foreignColumn , $ localColumn ) ; $ foreignTable -> addHasManyRelation ( $ localTableClass , $ foreignMethodName , $ localColumn , $ foreignColumn ) ; } } } }
Generate relation to each table

@param \ViKon\DbExporter\Meta\Model[] $models
Generate relation to each table