diff --git a/src/Console/GenerateCommand.php b/src/Console/GenerateCommand.php index 57f814e..d167e8d 100644 --- a/src/Console/GenerateCommand.php +++ b/src/Console/GenerateCommand.php @@ -243,7 +243,7 @@ protected function getPropertiesFromTable($model) $name !== $model::UPDATED_AT ) { if (!method_exists($model, 'getDeletedAtColumn') || (method_exists($model, 'getDeletedAtColumn') && $name !== $model->getDeletedAtColumn())) { - $this->setProperty($name, $type, $table); + $this->setProperty($model, $name, $type, $table); } } } @@ -277,7 +277,7 @@ protected function getPropertiesFromMethods($model) if ($pos = stripos($code, $search)) { $relationObj = $model->$method(); if ($relationObj instanceof Relation) { - $this->setProperty($relationObj->getForeignKeyName(), 'factory(' . get_class($relationObj->getRelated()) . '::class)'); + $this->setProperty($model, $relationObj->getForeignKeyName(), 'factory(' . get_class($relationObj->getRelated()) . '::class)'); } } } @@ -289,7 +289,7 @@ protected function getPropertiesFromMethods($model) * @param string $name * @param string|null $type */ - protected function setProperty($name, $type = null, $table = null) + protected function setProperty($model, $name, $type = null, $table = null) { if ($type !== null && Str::startsWith($type, 'factory(')) { $this->properties[$name] = $type; @@ -297,23 +297,6 @@ protected function setProperty($name, $type = null, $table = null) return; } - $fakeableTypes = [ - 'enum' => '$faker->randomElement(' . $this->enumValues($table, $name) . ')', - 'string' => '$faker->word', - 'text' => '$faker->text', - 'date' => '$faker->date()', - 'time' => '$faker->time()', - 'guid' => '$faker->word', - 'datetimetz' => '$faker->dateTime()', - 'datetime' => '$faker->dateTime()', - 'integer' => '$faker->randomNumber()', - 'bigint' => '$faker->randomNumber()', - 'smallint' => '$faker->randomNumber()', - 'decimal' => '$faker->randomFloat()', - 'float' => '$faker->randomFloat()', - 'boolean' => '$faker->boolean' - ]; - $fakeableNames = [ 'city' => '$faker->city', 'company' => '$faker->company', @@ -354,6 +337,31 @@ protected function setProperty($name, $type = null, $table = null) return; } + $enumValues = $this->enumValues($model, $table, $name); + + $fakeableTypes = [ + 'enum' => '$faker->randomElement(' . $enumValues . ')', + 'string' => '$faker->word', + 'text' => '$faker->text', + 'date' => '$faker->date()', + 'time' => '$faker->time()', + 'guid' => '$faker->word', + 'datetimetz' => '$faker->dateTime()', + 'datetime' => '$faker->dateTime()', + 'integer' => '$faker->randomNumber()', + 'bigint' => '$faker->randomNumber()', + 'smallint' => '$faker->randomNumber()', + 'decimal' => '$faker->randomFloat()', + 'float' => '$faker->randomFloat()', + 'boolean' => '$faker->boolean' + ]; + + if ($enumValues !== '[]') { + $this->properties[$name] = $fakeableTypes['enum']; + + return; + } + if (isset($fakeableTypes[$type])) { $this->properties[$name] = $fakeableTypes[$type]; @@ -363,19 +371,44 @@ protected function setProperty($name, $type = null, $table = null) $this->properties[$name] = '$faker->word'; } - public static function enumValues($table, $name) + public function enumValues($model, $table, $name) { if ($table === null) { return "[]"; } - $type = DB::select(DB::raw('SHOW COLUMNS FROM ' . $table . ' WHERE Field = "' . $name . '"'))[0]->Type; + $driver = $model->getConnection()->getDriverName(); + $values = null; + + if ($driver === 'mysql') { + $type = DB::connection($model->getConnectionName()) + ->select(DB::raw('SHOW COLUMNS FROM ' . $table . ' WHERE Field = "' . $name . '"'))[0]->Type; - preg_match_all("/'([^']+)'/", $type, $matches); + preg_match_all("/'([^']+)'/", $type, $matches); - $values = isset($matches[1]) ? $matches[1] : array(); + $values = isset($matches[1]) ? $matches[1] : null; + } else if ($driver === 'pgsql') { + $types = DB::connection($model->getConnectionName()) + ->select(DB::raw(" + select matches[1] + from pg_constraint, regexp_matches(pg_get_constraintdef(\"oid\"), '''(.+?)''', 'g') matches + where contype = 'c' + and conname = '{$table}_{$name}_check' + and conrelid = 'public.{$table}'::regclass; + ")); + + if (count($types)) { + $values = array(); + + foreach ($types as $type){ + $values[] = $type->matches; + } + } + } - return "['" . implode("', '", $values) . "']"; + return $values + ? "['" . implode("', '", $values) . "']" + : "[]"; }