Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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