0

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.

cronfy
  • 1,001
  • 1
  • 15
  • 32

2 Answers2

2

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.

enter image description here

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

enter image description here

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

Community
  • 1
  • 1
Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
  • Thank you for your answer! This method also requires lot of manual work, especially when many methods were added to the model. Currently I am trying to create CRUD in separate folder and extend my model/controller from it. But views can not be extended... – cronfy Jan 21 '17 at 08:48
  • It appears that my method still requires copying code from diffs... even if I extend my model from generated model, I need to override rules and relation methods, otherwise I get wrong (not extended) models from relation methods. Thus appears that your way is the only working one. Thanks! But in addition to what you suggest, I still use not GII generated models, but my own models, extended for generated ones (to keep autocompletion in IDE). – cronfy Jan 25 '17 at 10:52
  • Works like a charm – Paresh P. Feb 01 '17 at 12:25
0

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.

uldis
  • 344
  • 2
  • 6