A model is class that represents a logical structure and associated database table. In other words, models manage data.
EE has a number of models already there, like Channel, ChannelEntry, Member, Template, etc.
- Create an Entry
system/ee/EllisLab/ExpressionEngine/Controller/Publish/Publish.php
- Read a Member
system/ee/EllisLab/ExpressionEngine/Controller/Members/Members.php
- Get a File with Relationship
system/ee/EllisLab/ExpressionEngine/Controller/Files/Files.php
Let’s help E720 change their site so people can upvote and downvote their content
### Create our Data and DB Tables
- Models don't create your database tables by default, so we'll need to use the DBForge to create them.
1. Map our data first
2. Create schema in `upd` file
### Create our Data and DB Tables
- Models don't create your database tables by default, so we'll need to use the DBForge to create them.
1. Map our data first
2. Create schema in `upd` file
### Initiate Our Model
Models are registerd in your addon.setup.php file
Add it to your add-on setup
1. `models` array
2. Lots of ways to extend this, we'll go through one way later
### Initiate Our Model
Models are registerd in your addon.setup.php file
Add it to your add-on setup
1. `models` array
2. Lots of ways to extend this, we'll go through one way later
### Scaffolding
- Let's look at the basics of the model
1. Extend the EE Model class
2. Primary Key and Table Name are required
3. Protected Elements of the Model are your data points
### Let's Make it CRUDdy!
1. `votes` method: Reading the votes, Creating a new one
2. `cast_vote` method: Updating the votes
### Relationships
- By nature of our Votes model, it's connected to a ChannelEntry.
- Only problem is that we have not defined this relationship, so we would need multiple DB calls to get it.
- Relationships relate entries
1. Define the relationship
2. Define the inverse (addon.setup and Vote model)
3. `top` method: Getting entries by the top voted
## What Else Can Models Do?
- Events (i.e. update another model when your model is updated)
- Validation (i.e. check to make sure data coming in is good and will save)
- Metadata (i.e. setting variables re: whether model was updated)
- Relationships with pivot tables (i.e. tagging entries, multiple entries can have multiple tags)
- Collections (manage multiple items in your data)