Hello,
I'm trying to build a spark Command but i'm having trouble with CLI::prompt
PHP Code:
<?php namespace App\Commands ;
use CodeIgniter\CLI\BaseCommand ;
use CodeIgniter\CLI\CLI ;
class CreateModel extends BaseCommand {
protected $group = 'Create' ;
protected $name = 'create:model' ;
protected $description = 'Création d’un fichier app/Models/XxxxModel.php, arguments : model_name , table_name[=model_name]' ;
protected $usage = 'php spark create:model [model_name] ?[table_name]' ;
protected $arguments = [
'model_name' => 'le nom du model' ,
'table_name' => 'le nom de la table sans le préfixe, optionnel si le nom de la table est différent du nom du model'
] ;
protected $options = [] ;
public function run(array $params){
if( ! is_array($params) OR ! isset($params[0])){
CLI::error('argument model_name manquant') ;
CLI::newLine() ;
return false ;
}
helper('inflector');
$model_name = pascalize($params[0].'_model') ;
$model_filename = $model_name ;
$table_name = (isset($params[1])) ? $params[1] : $params[0] ;
# on verifie si la table existe
$db = \Config\Database::connect() ;
$table_exists = $db->tableExists($table_name) ;
if( ! $table_exists){
CLI::error('la table '.$table_name.' n’existe pas encore dans la base de données') ;
CLI::newLine() ;
return false ;
}
$fields = $db->getFieldData($table_name) ;
$fields_names = array_column($fields,'name') ;
$primary_key = CLI::prompt('Primary Key ?', $fields_names) ;
$return_type = CLI::prompt('Return Type ? (array | object | __EntityClassName__)',null, 'required') ;
$file_content = "<?php namespace App\Models ;".PHP_EOL
."use CodeIgniter\Model ;".PHP_EOL
."class ".$model_name." extends Model {".PHP_EOL ;
$file_content .= "}".PHP_EOL ;
CLI::write($file_content) ;
}
}
that gives :
Code:
CodeIgniter CLI Tool - Version 4.0.3 - Server-Time: 2020-05-27 12:41:37pm
Primary Key ? [id, email, password, level, civilite, prenom, nom, sign, tel, add1, add2, cp, ville, pays, created_at, updated_at, deleted_at]:
Return Type ? (array | object | EntityClassName) : array
Le champ Return Type ? (array | object | EntityClassName) est requis. (tr: the field is required)
Return Type ? (array | object | EntityClassName) : "array" ;
Le champ Return Type ? (array | object | EntityClassName) est requis. (tr: the field is required)
Return Type ? (array | object | EntityClassName) :
It looks that all kind of validation rule does not work.
is there a special class to write "use" for validation to work ?
What am I coding wrong ?
(sorry my english is bad)