Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Command/FixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
use Cake\Core\Configure;
use Cake\Core\Exception\CakeException;
use Cake\Database\Schema\TableSchemaInterface;
use Cake\Database\Type\EnumType;
use Cake\Database\TypeFactory;
use Cake\Datasource\ConnectionManager;
use Cake\Utility\Inflector;
use Cake\Utility\Text;
use DateTimeInterface;
use ReflectionEnum;

/**
* Task class for creating and updating fixtures files.
Expand Down Expand Up @@ -419,6 +422,33 @@ protected function _generateRecords(TableSchemaInterface $table, int $recordCoun
$insert = Text::uuid();
break;
}
if (str_starts_with($fieldInfo['type'], 'enum-')) {
$insert = null;
if ($fieldInfo['default'] || $fieldInfo['null'] === false) {
$dbType = TypeFactory::build($fieldInfo['type']);
if ($dbType instanceof EnumType) {
$class = $dbType->getEnumClassName();
$reflectionEnum = new ReflectionEnum($class);
$backingType = (string)$reflectionEnum->getBackingType();

if ($fieldInfo['default'] !== null) {
$insert = $fieldInfo['default'];
if ($backingType === 'int') {
$insert = (int)$insert;
}
} else {
$cases = $reflectionEnum->getCases();
if ($cases) {
$firstCase = array_shift($cases);
/** @var \BackedEnum $firstValue */
$firstValue = $firstCase->getValue();
$insert = $firstValue->value;
}
}
}
}
}

$record[$field] = $insert;
}
$records[] = $record;
Expand Down