CodeIgniter Forums
Add new option for make:model for specific table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Add new option for make:model for specific table (/showthread.php?tid=88137)



Add new option for make:model for specific table - datamweb - 07-27-2023

Usually, after building the model, I have to add the names of the fields to $allowedFields.This is time-consuming for me.

I don't know exactly whether other developers have experienced this issue or not.

I suggest to add a new option to command php spark make:modelfor this issue.

Option -for takes the name of the table and adds the fields to $allowedFields
to generate the model.

I think this option helps a lot in saving the developer's time.

Code:
Data of Table "sms_sender":

+----+--------------+------+------+----+---------------+--------+------------+------------+------------+
| id | message_text | type | from | to | send_datetime | status | created_at | updated_at | deleted_at |
+----+--------------+------+------+----+---------------+--------+------------+------------+------------+


Code:
php spark make:model -for sms_sender

PHP Code:
    protected $DBGroup          'default';
    protected $table            'sms_sender';
    protected $primaryKey      'id';
    protected $useAutoIncrement true;
    protected $returnType      'array';
    protected $useSoftDeletes  true;
    protected $protectFields    true;
    protected $allowedFields    = [
        'message_text',
        'type',
        'from',
        'to',
        'send_datetime',
        'status'
    ]; 



RE: Add new option for make:model for specific table - iRedds - 07-27-2023

I like this idea.


RE: Add new option for make:model for specific table - kenjis - 07-27-2023

No problem. Good idea!


RE: Add new option for make:model for specific table - ozornick - 07-27-2023

I agree. I myself had tables of the type date_1, date_N... When creating, it is important to take into account the primary key


RE: Add new option for make:model for specific table - InsiteFX - 07-27-2023

Yes, good idea but somefields will not need to be in allowFields.

So we will need to look into that.


RE: Add new option for make:model for specific table - luckmoshy - 07-27-2023

i agree too @datamweb


RE: Add new option for make:model for specific table - ozornick - 07-28-2023

(07-27-2023, 10:35 PM)InsiteFX Wrote: Yes, good idea but somefields will not need to be in allowFields.

So we will need to look into that.
For example? Default dates can be added (created_at, etc). ID as the `id` field to ignore. There is a problem if the ID is of a different type (`product_id`). Then you need to request it from the console. After creating the model, remind the developer to check these fields


RE: Add new option for make:model for specific table - iRedds - 07-28-2023

ID and timestamps are not needed in $allowedFields.


RE: Add new option for make:model for specific table - FabriceL - 07-31-2023

I also agree that it's a good idea.


RE: Add new option for make:model for specific table - kenjis - 10-31-2023

The option --table was implemented.

Code:
$ php spark make:model --help

CodeIgniter v4.4.3 Command Line Tool - Server Time: 2023-11-01 02:59:10 UTC+00:00

Usage:
  make:model <name> [options]

Description:
  Generates a new model file.

Arguments:
  name  The model class name.

Options:
  --table      Supply a table name. Default: "the lowercased plural of the class name".
  --dbgroup    Database group to use. Default: "default".
  --return    Return type, Options: [array, object, entity]. Default: "array".
  --namespace  Set root namespace. Default: "APP_NAMESPACE".
  --suffix    Append the component title to the class name (e.g. User => UserModel).
  --force      Force overwrite existing file.