Answers.Body: <p>For starters, I believe the Cocktail Recipes plugin is grossly out of date. It was originally built when Craft was in beta, and hasn't really been updated much since. There's a rumor that someone has forked &amp; updated it, but I don't have the full story there.</p>
  
  <hr>
  
  <p>Regarding your actual question...</p>
  
  <p><strong>Models</strong> - Think of a Model as a "jar" of related data, about one specific thing. This parallels pretty closely to most other programming languages and design patterns. As for Craft specifically, the Model is really the <em>right</em> way to pass a related group of data between components (Controllers, Services, etc).</p>
  
  <p><strong>Variables</strong> - A plugin Variable is simply a way to make your data easily available to front-end Twig templates. A large majority of the time, a Variable is simply a wrapper for some other method (like a Service call).</p>
  
  <p><strong>Services</strong> - Services can be anything and everything. It's a complete blank slate, and really the best place for your plugin to do any heavy lifting. If you ever have to interact with the database directly (via Records), it's virtually required that you run that DB interaction through a Service method.</p>
  
  <hr>
  
  <p>Here are two resources which may help shed a little more light on how it all ties together...</p>
  
  <p>Check out the <a href="https://github.com/lindseydiloreto/craft-businesslogic" rel="nofollow noreferrer">Business Logic plugin template...</a> Originally intended as a quick way to insert business logic into your site via PHP, it's become a useful learning resource as well. It focuses mainly on Variables, Services, and Controllers, and is completely documented in the code comments.</p>
  
  <p>Lastly, here's a diagram which I put together as I was learning plugin development. Keep in mind, this is not a 100% 1:1 representation of every-plugin-ever. Any given plugin can contain more or less of these components... this is simply a basic illustration of how the components tie together.</p>
  
  <p><img src="https://i.sstatic.net/2NTDZ.png" alt="Craft Plugin Structure"></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-03T19:39:16.167Z
Answers.LastActivityDate: 2015-05-03T19:39:16.167Z
Answers.UserDisplayName: Lindsey D
Body: <p>I am new to Craft and have been attempting to write  a plugin but keep getting stuck on how to read and write to the database. I have been working through cocktailrecipesPlugin and am attemting to use it to better understand how to do database CRUD. In this attempt I am replacing coctailrecipes with projectmanager, ingredients with staff, and ingredient with staffMember. The Cocktail Recipe plugin is confusing because it uses objects and properties with the same name and I am unsure if this is the required. I am having difficulty understanding how services and variable and models interact. It appears that services is where the property ingredient is defined, but I am puzzled. I wish there was better documentation explaining the interaction of the various components. I have been banging my head against the wall trying to understand the Craft api and how the pieces work together. The documenation seem sparse and the code sample in the documenation are snippets which make it difficult to understand how everything fits together. I wish the cocktailRecipiesPlugin was better explained. </p>
  
  <p>Cocktail Recipes &lt;=> Project Manager</p>
  
  <p>ingredients &lt;=> staff</p>
  
  <p>ingredient &lt;=> staffmember</p>
  
Tags: <plugin-development>
Title: How do variables, services and models fit together?
ThreadText: # How do variables, services and models fit together?
  
  (Asked by: username_0 on 2015-05-02)
  
  I am new to Craft and have been attempting to write a plugin but keep getting stuck on how to read and write to the database. I have been working through cocktailrecipesPlugin and am attemting to use it to better understand how to do database CRUD. In this attempt I am replacing coctailrecipes with projectmanager, ingredients with staff, and ingredient with staffMember. The Cocktail Recipe plugin is confusing because it uses objects and properties with the same name and I am unsure if this is the required. I am having difficulty understanding how services and variable and models interact. It appears that services is where the property ingredient is defined, but I am puzzled. I wish there was better documentation explaining the interaction of the various components. I have been banging my head against the wall trying to understand the Craft api and how the pieces work together. The documenation seem sparse and the code sample in the documenation are snippets which make it difficult to understand how everything fits together. I wish the cocktailRecipiesPlugin was better explained.
  
  Cocktail Recipes <=> Project Manager
  
  ingredients <=> staff
  
  ingredient <=> staffmember
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-05-03. Score: 9
  
  For starters, I believe the Cocktail Recipes plugin is grossly out of date. It was originally built when Craft was in beta, and hasn't really been updated much since. There's a rumor that someone has forked & updated it, but I don't have the full story there.
  
  ---
  
  Regarding your actual question...
  
  **Models** - Think of a Model as a "jar" of related data, about one specific thing. This parallels pretty closely to most other programming languages and design patterns. As for Craft specifically, the Model is really the *right* way to pass a related group of data between components (Controllers, Services, etc).
  
  **Variables** - A plugin Variable is simply a way to make your data easily available to front-end Twig templates. A large majority of the time, a Variable is simply a wrapper for some other method (like a Service call).
  
  **Services** - Services can be anything and everything. It's a complete blank slate, and really the best place for your plugin to do any heavy lifting. If you ever have to interact with the database directly (via Records), it's virtually required that you run that DB interaction through a Service method.
  
  ---
  
  Here are two resources which may help shed a little more light on how it all ties together...
  
  Check out the [Business Logic plugin template...](https://github.com/lindseydiloreto/craft-businesslogic) Originally intended as a quick way to insert business logic into your site via PHP, it's become a useful learning resource as well. It focuses mainly on Variables, Services, and Controllers, and is completely documented in the code comments.
  
  Lastly, here's a diagram which I put together as I was learning plugin development. Keep in mind, this is not a 100% 1:1 representation of every-plugin-ever. Any given plugin can contain more or less of these components... this is simply a basic illustration of how the components tie together.
  
  ![Craft Plugin Structure](https://i.sstatic.net/2NTDZ.png)
  
  
  ---
  
  