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?
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityThe 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.
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?