mootensai\enhancedgii\BaseGenerator::generateManyManyRelations PHP Method

generateManyManyRelations() private method

Generates relations using a junction table by adding an extra viaTable().
private generateManyManyRelations ( yii\db\TableSchema $table, array $fks, array $relations ) : array
$table yii\db\TableSchema
$fks array obtained from the checkPivotTable() method
$relations array
return array modified $relations
    private function generateManyManyRelations($table, $fks, $relations)
    {
        $db = $this->getDbConnection();
        $table0 = $fks[$table->primaryKey[0]][0];
        $table1 = $fks[$table->primaryKey[1]][0];
        $className0 = $this->generateClassName($table0);
        $className1 = $this->generateClassName($table1);
        $table0Schema = $db->getTableSchema($table0);
        $table1Schema = $db->getTableSchema($table1);
        $link = $this->generateRelationLink([$fks[$table->primaryKey[1]][1] => $table->primaryKey[1]]);
        $viaLink = $this->generateRelationLink([$table->primaryKey[0] => $fks[$table->primaryKey[0]][1]]);
        $relationName = $this->generateRelationName($relations, $table0Schema, $table->primaryKey[1], true);
        $relations[$table0Schema->fullName][$relationName] = ["return \$this->hasMany(\\{$this->nsModel}\\{$className1}::className(), {$link})->viaTable('" . $this->generateTableName($table->name) . "', {$viaLink});", $className1, true];
        $link = $this->generateRelationLink([$fks[$table->primaryKey[0]][1] => $table->primaryKey[0]]);
        $viaLink = $this->generateRelationLink([$table->primaryKey[1] => $fks[$table->primaryKey[1]][1]]);
        $relationName = $this->generateRelationName($relations, $table1Schema, $table->primaryKey[0], true);
        $relations[$table1Schema->fullName][$relationName] = ["return \$this->hasMany(\\{$this->nsModel}\\{$className0}::className(), {$link})->viaTable('" . $this->generateTableName($table->name) . "', {$viaLink});", $className0, true];
        return $relations;
    }