Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In CakePHP, models handle all data responsibilities, including loading, saving, and validation. A model class represents data, and typically is on a one to one relationship with the database table. In that way, CakePHP's models are an ORM, an object relational mapper. Which in a practical sense means that all data based interactions go through the model. Additionally, a model can be associated with other models, allowing for simple and complex relationships.
Cake models provide common functionality, including Create, Read, Update and Destroy operations, which is commonly known as CRUD. This functionality is unlocked by extending Cake's internal model class, AppModel. I'll demonstrate that shortly. To put this in context of our catalog application, magazine back issues have a number of common properties. For the purposes of this course, I'm going to limit that domain to two models. One for the publication as a unifying record, and the second for the issues themselves.
A publication has many issues and every issue belongs to a particular publication. Each publication has a unique identifier, the publication ID, which is an integer and the primary key. It also has one other column, the publication name. The magazine back issue is a little bit more complex. Unique identifier, the issue ID, then the foreign key to publications. There's a issue number, which is the number given by the publisher, like issue number one.
The issue date publication, which is when it was published. And finally, the path to the file containing the cover. Cake has some specific naming conventions that should be followed when representing and storing data. Model class names are singular and camel cased without any spaces. So if there's multiple words in the model name, each starts with a capital letter. Therefore, we'll have publication and issue. Data base table names also have a naming convention, but it's a little different. They're plural, lower case and underscored for separation.
Therefore, the table names we're using are publications and issues. Why have a standard other than readability and developer communication? Well, Cake can read database structures and generate code based on them, which is a fantastic time saver. Let's create the models for our application.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101728 Viewers
61 Video lessons · 88489 Viewers
71 Video lessons · 72307 Viewers
56 Video lessons · 104022 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.