yii\gii\generators\model\Generator::generateManyManyRelations PHP Method

generateManyManyRelations() private method

Generates relations using a junction table by adding an extra viaTable().
private generateManyManyRelations ( $table, array $fks, array $relations ) : array
$fks array obtained from the checkJunctionTable() method
$relations array
return array modified $relations
    private function generateManyManyRelations($table, $fks, $relations)
    {
        $db = $this->getDbConnection();
        foreach ($fks as $pair) {
            list($firstKey, $secondKey) = $pair;
            $table0 = $firstKey[0];
            $table1 = $secondKey[0];
            unset($firstKey[0], $secondKey[0]);
            $className0 = $this->generateClassName($table0);
            $className1 = $this->generateClassName($table1);
            $table0Schema = $db->getTableSchema($table0);
            $table1Schema = $db->getTableSchema($table1);
            // @see https://github.com/yiisoft/yii2-gii/issues/166
            if ($table0Schema === null || $table1Schema === null) {
                continue;
            }
            $link = $this->generateRelationLink(array_flip($secondKey));
            $viaLink = $this->generateRelationLink($firstKey);
            $relationName = $this->generateRelationName($relations, $table0Schema, key($secondKey), true);
            $relations[$table0Schema->fullName][$relationName] = ["return \$this->hasMany({$className1}::className(), {$link})->viaTable('" . $this->generateTableName($table->name) . "', {$viaLink});", $className1, true];
            $link = $this->generateRelationLink(array_flip($firstKey));
            $viaLink = $this->generateRelationLink($secondKey);
            $relationName = $this->generateRelationName($relations, $table1Schema, key($firstKey), true);
            $relations[$table1Schema->fullName][$relationName] = ["return \$this->hasMany({$className0}::className(), {$link})->viaTable('" . $this->generateTableName($table->name) . "', {$viaLink});", $className0, true];
        }
        return $relations;
    }