Esempio n. 1
0
';
        }
    }
}
// use
if (array_key_exists($tableName, $relationUses) && in_array('yii\\db\\Expression', $relationUses[$tableName])) {
    $dbExpression = 'Expression';
} else {
    $dbExpression = '\\yii\\db\\Expression';
}
// list items
foreach ($tableSchema->foreignKeys as $foreignKey) {
    $foreignTableName = $foreignKey[0];
    unset($foreignKey[0]);
    /* @var $foreignModelClass string|\yii\boost\db\ActiveRecord */
    $foreignModelClass = Helper::getModelClassByTableName($foreignTableName);
    if ($foreignModelClass && class_exists($foreignModelClass)) {
        $primaryKey = $foreignModelClass::primaryKey();
        if (count($primaryKey) == 1) {
            $attribute = array_search($primaryKey[0], $foreignKey);
            if ($attribute) {
                $attributeArg = Inflector::variablize($attribute);
                $listItemConditions = [];
                if (count($foreignKey) > 1) {
                    foreach (array_diff($foreignKey, $primaryKey) as $key1 => $key2) {
                        $listItemConditions[] = '\'' . $key2 . '\' => $this->' . $key1;
                    }
                    if (count($listItemConditions) == 1) {
                        $listItemConditions = $listItemConditions[0];
                    } else {
                        $listItemConditions = '
Esempio n. 2
0
 /**
  * @inheritdoc
  */
 protected function generateRelations()
 {
     $db = $this->getDbConnection();
     $relations = [];
     $this->relationUses = [];
     $this->allRelations = [];
     $this->singularRelations = [];
     $this->pluralRelations = [];
     $generatedRelations = parent::generateRelations();
     foreach ($generatedRelations as $tableName => $tableRelations) {
         /* @var $tableSchema \yii\gii\plus\db\TableSchema */
         $tableSchema = $db->getTableSchema($tableName);
         $relations[$tableName] = [];
         $this->relationUses[$tableName] = [];
         $this->allRelations[$tableName] = [];
         $this->singularRelations[$tableName] = [];
         $this->pluralRelations[$tableName] = [];
         foreach ($tableRelations as $relationName => $relation) {
             list($code, $className, $hasMany) = $relation;
             /* @var $nsClassName string|\yii\boost\db\ActiveRecord */
             $nsClassName = Helper::getModelClassByTableName(array_search($className, $this->classNames));
             if ($nsClassName && class_exists($nsClassName)) {
                 $relations[$tableName][$relationName] = [$code, $className, $hasMany];
                 $this->relationUses[$tableName][] = $nsClassName;
                 // extended relations
                 $subTableSchema = $nsClassName::getTableSchema();
                 $subTableName = $subTableSchema->fullName;
                 // link
                 $link = [];
                 $direct = null;
                 if ($hasMany) {
                     foreach ($subTableSchema->foreignKeys as $foreignKey) {
                         if ($foreignKey[0] == $tableName) {
                             unset($foreignKey[0]);
                             $refs = $foreignKey;
                             if (strpos($code, $this->generateRelationLink($refs)) != false) {
                                 $link = $refs;
                                 $direct = false;
                                 break;
                             }
                         }
                     }
                 } else {
                     foreach ($tableSchema->foreignKeys as $foreignKey) {
                         if ($foreignKey[0] == $subTableName) {
                             unset($foreignKey[0]);
                             $refs = array_flip($foreignKey);
                             if (strpos($code, $this->generateRelationLink($refs)) != false) {
                                 $link = $refs;
                                 $direct = true;
                                 break;
                             }
                         }
                     }
                     if (!count($link)) {
                         foreach ($subTableSchema->foreignKeys as $foreignKey) {
                             if ($foreignKey[0] == $tableName) {
                                 unset($foreignKey[0]);
                                 $refs = $foreignKey;
                                 if (strpos($code, $this->generateRelationLink($refs)) != false) {
                                     $link = $refs;
                                     $direct = false;
                                     break;
                                 }
                             }
                         }
                     }
                 }
                 $viaTable = false;
                 if (preg_match('~\\-\\>viaTable\\(\'(\\w+)(?:\'| )~', $code, $match)) {
                     $viaTable = $match[1];
                 }
                 $linkCode = $this->generateRelationLink($link);
                 $this->allRelations[$tableName][$relationName] = compact('code', 'className', 'hasMany', 'nsClassName', 'link', 'direct', 'viaTable', 'linkCode');
                 if ($hasMany) {
                     $this->pluralRelations[$tableName][$relationName] = compact('code', 'className', 'hasMany', 'nsClassName', 'link', 'direct', 'viaTable', 'linkCode');
                 } else {
                     $this->singularRelations[$tableName][$relationName] = compact('code', 'className', 'hasMany', 'nsClassName', 'link', 'direct', 'viaTable', 'linkCode');
                 }
                 // via relations
                 if (!$hasMany && $subTableName != $tableName) {
                     foreach ($generatedRelations[$subTableName] as $subRelationName => $subRelation) {
                         list($subCode, $subClassName, $subHasMany) = $subRelation;
                         $tableName2 = array_search($subClassName, $this->classNames);
                         if ($tableName2 != $tableName) {
                             /* @var $subNsClassName string|\yii\boost\db\ActiveRecord */
                             $subNsClassName = Helper::getModelClassByTableName($tableName2);
                             if ($subNsClassName && class_exists($subNsClassName)) {
                                 if (!$subHasMany && !array_key_exists($subRelationName, $generatedRelations[$tableName])) {
                                     $viaLink = $this->generateRelationLink($link);
                                     $subCode = preg_replace('~;$~', "\n" . '            ->viaTable(\'' . $subTableName . ' via_' . $subTableName . '\', ' . $viaLink . ');', $subCode);
                                     if (!array_key_exists($subRelationName, $relations[$tableName]) || $direct) {
                                         $relations[$tableName][$subRelationName] = [$subCode, $subClassName, $subHasMany];
                                         $this->relationUses[$tableName][] = $subNsClassName;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $this->relationsDone = true;
     $this->classNames = [];
     return $this->fixRelations($relations);
 }