Skip to content

Commit

Permalink
Merge pull request #544 from thiagotalma/relation_name
Browse files Browse the repository at this point in the history
Use the name of the table to create the relation
  • Loading branch information
bizley committed Nov 22, 2023
2 parents e65f3b2 + 7762080 commit c7533e5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/generators/model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,21 @@ public function generateRules($table)
{
$types = [];
$lengths = [];
$nullable = [];
$defaultValues = [];
foreach ($table->columns as $column) {
if ($column->autoIncrement) {
continue;
}
if (!$column->allowNull && $column->defaultValue === null) {
$types['required'][] = $column->name;
} elseif ($column->allowNull && $column->defaultValue === null) {
$nullable[] = $column->name;
} elseif (is_scalar($column->defaultValue)) {
if (array_key_exists($column->defaultValue, $defaultValues)) {
$defaultValues[$column->defaultValue] = [];
}
$defaultValues[$column->defaultValue][] = $column->name;
}
switch ($column->type) {
case Schema::TYPE_SMALLINT:
Expand Down Expand Up @@ -480,6 +489,15 @@ public function generateRules($table)
}
}
$rules = [];
if (!empty($nullable)) {
$rules[] = "[['" . implode("', '", $nullable) . "'], 'default', 'value' => null]";
}
if (!empty($defaultValues)) {
foreach ($defaultValues as $defaultValue => $defaultValueColumns) {
$defaultValue = is_numeric($defaultValue) ? $defaultValue : "'$defaultValue'";
$rules[] = "[['" . implode("', '", $defaultValueColumns) . "'], 'default', 'value' => $defaultValue]";
}
}
$driverName = $this->getDbDriverName();
foreach ($types as $type => $columns) {
if ($driverName === 'pgsql' && $type === 'integer') {
Expand Down

0 comments on commit c7533e5

Please sign in to comment.