1

I'm working on a plugin, and would like to add a new Record, I already have some records, and would like to keep the data.

Is there a way to activate a new record type, without re-installing the plugin, thus loosing the other records data?

2

The Craft website has a great article on this, you can simply use migrations following this document:

https://craftcms.com/docs/plugins/migrations

If your schema changes over the life of your plugin, you can write a migration to keep existing installations updated with the latest schema. Craft automatically checks for new migrations whenever a plugin’s version number changes.

In summary, you can create the Record and define all the attributes, then create a migration that would create the new table with those attributes. This would be done in the safeUp() method that utilizes Craft create command:

craft()->db->createCommand()->createTable('new_table_record_name', $attributesArray, null, true);

When the plugin is updated, it will run the migrations and create the table.

| improve this answer | |
  • I took a similar approach, but instead of using craft()->db->createCommand() I created an instance of the record model using the "install" scenario and then followed the lead from what plugins do when they install. So it was like $record = new PluginName_DataRecord('install'); $record->createTable(); $record->addForeignKeys(); Is there any advantage/disadvantage to this method? – John O Jul 15 '16 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.