Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the name of the table to create the relation #544

Merged
merged 17 commits into from
Nov 22, 2023
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
Loading