Skip to content

Commit

Permalink
Changed model creation using full class path for relations and rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
almirb committed May 11, 2018
1 parent 344e58f commit 19093b0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ protected function generateProperties($table)
private function generateManyManyRelations($table, $fks, $relations)
{
$db = $this->getDbConnection();
$nsModel = $this->nsModel;

foreach ($fks as $pair) {
list($firstKey, $secondKey) = $pair;
Expand All @@ -443,7 +444,7 @@ private function generateManyManyRelations($table, $fks, $relations)
$viaLink = $this->generateRelationLink($firstKey);
$relationName = $this->generateRelationName($relations, $table0Schema, key($secondKey), true);
$relations[$table0Schema->fullName][$relationName] = [
"return \$this->hasMany($className1::class, $link)->viaTable('"
"return \$this->hasMany(\\{$nsModel}\\{$className1}::class, $link)->viaTable('"
. $this->generateTableName($table->name) . "', $viaLink);",
$className1,
true,
Expand All @@ -453,7 +454,7 @@ private function generateManyManyRelations($table, $fks, $relations)
$viaLink = $this->generateRelationLink($secondKey);
$relationName = $this->generateRelationName($relations, $table1Schema, key($firstKey), true);
$relations[$table1Schema->fullName][$relationName] = [
"return \$this->hasMany($className0::class, $link)->viaTable('"
"return \$this->hasMany(\\{$nsModel}\\{$className0}::class, $link)->viaTable('"
. $this->generateTableName($table->name) . "', $viaLink);",
$className0,
true,
Expand All @@ -475,6 +476,7 @@ protected function generateRelations()
$db = $this->getDbConnection();
$relations = [];
$schemaNames = $this->getSchemaNames();
$nsModel = $this->nsModel;
foreach ($schemaNames as $schemaName) {
foreach ($db->getSchema()->getTableSchemas($schemaName) as $table) {
$className = $this->generateClassName($table->fullName);
Expand All @@ -493,7 +495,7 @@ protected function generateRelations()
$link = $this->generateRelationLink(array_flip($refs));
$relationName = $this->generateRelationName($relations, $table, $fks[0], false);
$relations[$table->fullName][$relationName] = [
"return \$this->hasOne($refClassName::class, $link);",
"return \$this->hasOne(\\{$nsModel}\\{$refClassName}::class, $link);",
$refClassName,
false,
];
Expand All @@ -502,8 +504,9 @@ protected function generateRelations()
$hasMany = $this->isHasManyRelation($table, $fks);
$link = $this->generateRelationLink($refs);
$relationName = $this->generateRelationName($relations, $refTableSchema, $className, $hasMany);
$nsModel = $this->nsModel;
$relations[$refTableSchema->fullName][$relationName] = [
"return \$this->" . ($hasMany ? 'hasMany' : 'hasOne') . "($className::class, $link);",
"return \$this->" . ($hasMany ? 'hasMany' : 'hasOne') . "(\\{$nsModel}\\{$className}::class, $link);",
$className,
$hasMany,
];
Expand Down Expand Up @@ -905,6 +908,7 @@ public function generateRules($table)
}

// Exist rules for foreign keys
$nsModel = $this->nsModel;
foreach ($table->foreignKeys as $refs) {
$refTable = $refs[0];
$refTableSchema = $db->getTableSchema($refTable);
Expand All @@ -920,7 +924,7 @@ public function generateRules($table)
$targetAttributes[] = "'$key' => '$value'";
}
$targetAttributes = implode(', ', $targetAttributes);
$rules[] = "[['$attributes'], 'exist', 'skipOnError' => true, 'targetClass' => $refClassName::class, 'targetAttribute' => [$targetAttributes]]";
$rules[] = "[['$attributes'], 'exist', 'skipOnError' => true, 'targetClass' => \\{$nsModel}\\{$refClassName}::class, 'targetAttribute' => [$targetAttributes]]";
}

return $rules;
Expand Down

0 comments on commit 19093b0

Please sign in to comment.