4

I am absolutely new to craft plugin development and could not find any documentation on creating database tables specific to a new plugin. How are new tables created when a plugin is installed?

I am browsing through the source code for the CocktailRecipes plugin: https://github.com/adrianmacneil/cocktailrecipes/ and I cannot find how the craft_cocktailrecipes_ingredients table gets created when I install it.

Is it merely a matter of defining a record class like CocktailRecipes_IngredientRecord and then overriding the getTableName and defineAttributes methods?

Is that how craft cms builds the tables?

2

Yep, that's it, check out the Records documentation to learn more:

Active record models (or “records”) are like models, except with a database-facing layer built on top. On top of all the things that models can do, records can:

  • Define database table schemas
  • Represent rows in the database
  • Find, alter, and delete rows

Also, you might find "Creating Database Migrations" interesting.

4

PluginNameRecord.php is how Craft installs database tables. You can also have multiple tables by using PluginName_TableRecord.php where 'Table' will be the name of the new table in the database.

Might also be worthwhile to mention that the cocktails recipe you linked to has a lot of issues as it has not been updated for the latest versions of Craft.

Sean Delaney updated that plugin and the new repository is here:

https://github.com/seandelaney/cocktailrecipes

  • The delete option doesn't work for the updated version. Any ideas why?? – Sandy D. Mar 21 '16 at 3: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.