Model is an important component of MVC design pattern, models are basically responsible for storing data and exchange with other layers in the application, in CakePHP models are fat layer and serve as business and data access layer. In addition to these roles’ models can manipulate the data by several other means, for example, files, web services etc.
Guys, if you don’t know what is MVC design pattern, visit my article What is the Design Pattern of CakePHP 4.
Creating Model
Let’s begin, open the visual studio code, if you have not installed visual studio code, use the command prompt to create the model in your application. It is good idea if you are serious with development then download any good code editor having PHP IntelliSense support. I recommend visual studio code, it is free community edition, rest is up to your convenience.
On the command prompt, navigate to your application folder and then in bin folder, in my case it is “c:\xampp\htdocs\myshop\bin”. Type the below command for customers table and see the magic.
The command has invoked the bake utility and created two important classes in their respective folders. The first model class is created under model\table with name CustomersTable and the other class under the model\entity folder with name Customer, both classes are model classes but different responsibilities. The other two classes are generated as part of testing framework and are not part of the discussion in this article.
Let’s see what is inside each class and how it works.
Model CustomersTable Class
CustomersTable class is extended from Table parent class, it inherits several useful features as part of standard package of ORM framework. Table class encapsulate the basic functionality of CURD operation and maintaining the association between multiple tables.
At the run time when controller requires the model for data insert, update, delete or retrieval, model knows how to handle the request and issue the query to underlying database. All this is handled through Table class provided by the ORM framework.
The first method in this class is Initialization, here ORM is initialized by setting the table name in the database “Customers”, its primary key name and its display name. These are the minimum configurations required to deal with the corresponding table in the database.
The second method is ValidationDefault, this method is responsible for the basic validation of the data fields received for insert or update.
Model Customer Class
The Customer class extends the Entity parent class, it stores the single row of the table for data manipulation. In this class we can adjust the accessibility of each field. This class is nothing more than just setting the model fields.
Today we have seen how to create the Model, which is responsible to manipulate data between business and data access layer, model is responsible for data validation. In next article we will see how different views work with models and controllers to visually present the data on customer browser.
Like and share my article, subscribe for latest articles in future.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.