简体   繁体   中英

Yii2: add field to autogenerated CRUD (automatically?)

Yii2 has a feature of generating CRUD for table with gii .

It creates several files: controller, model, search model and views.

What if after generating CRUD I need to add more fields to the table? I see that I need to change:

  • Model's
    • phpdoc
    • rules()
    • attributeLabels()
  • Search model's
    • rules()
    • search() (tweak ->andFilterWhere() calls)
  • Views:
    • index (grid columns)
    • _form (model inputs)
    • _search (search inputs)
    • view (attribute rows)

A lot of work. Is there a way to do it easier/automatically? I understand that I can just regenerate CRUD with gii, but in this case all my other tweaks would be overwritten (lost).

Maybe there exist some other CRUD solution for Yii2, that does not hardcode all fields and allows table schema changes to appear automatically in the views/models? Would be glad to know about it.

If you dont want to overwrite file or add changes manually , you have below option:

In Gii , after clicking preview button you can see list of files.

在此处输入图片说明

If file has changes or already in directory than diff button displayed. On clicking diff button you can see changes in file.

在此处输入图片说明

From this model (window) you can modify file as your need or you can copy/paste code in/from temporary file and manage.

Simple way is regenerate model and crud after adding fields to table. If you have modification in crud, you can generate in GUI files without saving and by copy paste move new column code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM