Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,987 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
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.
Speed up your development with CakePHP, the popular open-source PHP framework. In this course, author Jon Peck builds a magazine cataloging system while explaining how to work with the Model-View-Controller (MVC) development pattern.
The course demonstrates how to install and configure CakePHP, describing the layout and components. Then, leveraging the Bake console, Jon shows how to generate and customize code, and explores form validation, database persistence, and even image uploads. By developing a complete, functional application, you'll have the foundation you need to build your own applications with CakePHP.
- What is CakePHP?
- Installing and configuring CakePHP
- Generating a model with Bake
- Linking models together
- Generating a controller
- Saving a model from the controller
- Displaying a model with a view
- Debugging a failed save
- Using helpers to generate HTML
- Uploading files and displaying images