Skip to content
Merged
Show file tree
Hide file tree
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
60 changes: 39 additions & 21 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,45 @@ public function __construct(array $propiedades = [])
}
}

public function ask(array $previous): void
public function ask(array $previous, bool $existsPrimaryKey = false): void
{
while (true) {
// Construimos las opciones base
$options = [
'1' => 'serial (autonumérico, ideal para ids)',
'2' => 'integer',
'3' => 'float',
'4' => 'boolean',
'5' => 'character varying',
'6' => 'text',
'7' => 'timestamp',
'8' => 'date',
'9' => 'time'
];

// Si ya existe una primary key, quitamos la opción de serial
if ($existsPrimaryKey) {
unset($options['1']);
}

$type = (int)select(
label: 'Elija el tipo de campo',
options: [
// 'valor que devuelve' => 'key que se muestra al usuario a elegir'
'1' => 'serial (autonumérico, ideal para ids)',
'2' => 'integer',
'3' => 'float',
'4' => 'boolean',
'5' => 'character varying',
'6' => 'text',
'7' => 'timestamp',
'8' => 'date',
'9' => 'time'
],
default: '1',
scroll: 9, // cantidad de opciones a mostrar a la vez en pantalla (el resto scroll)
options: $options,
default: $existsPrimaryKey ? '2' : '1',
scroll: count($options), // cantidad de opciones a mostrar a la vez en pantalla (el resto scroll
required: true
);

if ($type === 1) {
// Si no existe primary key y el usuario eligió serial, verificamos que no haya otra
if (!$existsPrimaryKey && $type === 1) {
foreach ($previous as $column) {
if ($column->tipo === 'serial' || $column->primary) {
Utils::echo("\nYa hay un campo de tipo serial o primary key.\n");
continue 2;
}
}
}

if ($type >= 1 && $type <= 9) {
$this->setType($type);
break;
Expand All @@ -88,12 +97,12 @@ public function ask(array $previous): void
}
}

public static function askMulti(bool $extension = false): array
public static function askMulti(bool $existsPrimaryKey = false): array
{
$fields = [];

// si estamos en una extensión, no preguntamos por los campos por defecto
if (false === $extension) {
// si ya existe una primary key (por ejemplo, en extensiones), no preguntamos por los campos por defecto
if (false === $existsPrimaryKey) {
$prompt = Utils::promptYesOrNo(
label: '¿Desea crear los campos habituales? (No = Default)'
);
Expand Down Expand Up @@ -138,6 +147,15 @@ public static function askMulti(bool $extension = false): array
}

while (true) {
// Verificamos dinámicamente si ya existe una primary key en los campos actuales
$hasPrimaryKey = $existsPrimaryKey;
foreach ($fields as $field) {
if ($field->tipo === 'serial' || $field->primary) {
$hasPrimaryKey = true;
break;
}
}

$name = Utils::prompt(
label: 'Nombre del campo (vacío para terminar)',
placeholder: 'Dejar vacío o Ej: email',
Expand All @@ -156,7 +174,7 @@ public static function askMulti(bool $extension = false): array
}

$column = new Column(['nombre' => $name]);
$column->ask($fields);
$column->ask($fields, $hasPrimaryKey);

$fields[] = $column;
}
Expand All @@ -166,7 +184,7 @@ public static function askMulti(bool $extension = false): array
return strcmp($a->nombre, $b->nombre);
});

if (false === $extension) {
if (false === $existsPrimaryKey) {
self::askPrimaryKey($fields);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Command/Extension/ExtensionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ private function createExtensionTable(string $name): void
return;
}

// En extensiones, la tabla ya existe y por tanto ya tiene una primary key
$fields = Column::askMulti(true);
FileGenerator::createTableXmlByFields($fileName, $name, $fields);
Utils::echo('* ' . $fileName . " -> OK.\n");
Expand Down Expand Up @@ -208,6 +209,7 @@ private function createExtensionXMLView(string $name): void
$type = 'edit';
}

// En extensiones de XMLView, la tabla ya existe y por tanto ya tiene una primary key
$fields = Column::askMulti(true);
FileGenerator::createXMLViewByFields($fileName, $fields, $type, true);
Utils::echo('* ' . $fileName . " -> OK.\n");
Expand Down
Loading