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?

Amit Erandole
  • 338
  • 2
  • 12

2 Answers2

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

Jason McCallister
  • 2,336
  • 14
  • 27
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.

Victor
  • 8,376
  • 1
  • 34
  • 61