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

generateRelations() protected method

protected generateRelations ( ) : array
return array the generated relation declarations
    protected function generateRelations()
    {
        if ($this->generateRelations === self::RELATIONS_NONE) {
            return [];
        }
        $db = $this->getDbConnection();
        $relations = [];
        $schemaNames = $this->getSchemaNames();
        foreach ($schemaNames as $schemaName) {
            foreach ($db->getSchema()->getTableSchemas($schemaName) as $table) {
                $className = $this->generateClassName($table->fullName);
                foreach ($table->foreignKeys as $refs) {
                    $refTable = $refs[0];
                    $refTableSchema = $db->getTableSchema($refTable);
                    if ($refTableSchema === null) {
                        // Foreign key could point to non-existing table: https://github.com/yiisoft/yii2-gii/issues/34
                        continue;
                    }
                    unset($refs[0]);
                    $fks = array_keys($refs);
                    $refClassName = $this->generateClassName($refTable);
                    // Add relation for this table
                    $link = $this->generateRelationLink(array_flip($refs));
                    $relationName = $this->generateRelationName($relations, $table, $fks[0], false);
                    $relations[$table->fullName][$relationName] = ["return \$this->hasOne({$refClassName}::className(), {$link});", $refClassName, false];
                    // Add relation for the referenced table
                    $hasMany = $this->isHasManyRelation($table, $fks);
                    $link = $this->generateRelationLink($refs);
                    $relationName = $this->generateRelationName($relations, $refTableSchema, $className, $hasMany);
                    $relations[$refTableSchema->fullName][$relationName] = ["return \$this->" . ($hasMany ? 'hasMany' : 'hasOne') . "({$className}::className(), {$link});", $className, $hasMany];
                }
                if (($junctionFks = $this->checkJunctionTable($table)) === false) {
                    continue;
                }
                $relations = $this->generateManyManyRelations($table, $junctionFks, $relations);
            }
        }
        if ($this->generateRelations === self::RELATIONS_ALL_INVERSE) {
            return $this->addInverseRelations($relations);
        }
        return $relations;
    }

Usage Example

Example #1
0
 protected function generateRelations()
 {
     $relations = parent::generateRelations();
     // inject namespace
     $ns = "\\{$this->ns}\\";
     foreach ($relations as $model => $relInfo) {
         foreach ($relInfo as $relName => $relData) {
             $relations[$model][$relName][0] = preg_replace('/(has[A-Za-z0-9]+\\()([a-zA-Z0-9]+::)/', '$1__NS__$2', $relations[$model][$relName][0]);
             $relations[$model][$relName][0] = str_replace('__NS__', $ns, $relations[$model][$relName][0]);
         }
     }
     return $relations;
 }
All Usage Examples Of yii\gii\generators\model\Generator::generateRelations