Tuesday, July 21, 2020

How to create Model in CakePHP 4?

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.

Related Posts:

  • What is CakePHP 4 Development Strategy?Hi guys, in the last article we have discussed the design pattern of CakePHP 4.0, today we will discuss the development strategy. When we talk about strategy, first question comes in the mind from where we should start. So th… Read More
  • What is CakePHP 4?What is CakePHP? CakePHP is web application development framework based on PHP (Hypertext Processor). It is an open source scripting language and widely supported by the developers communities. It follows the MVC (Model,… Read More
  • How to create First CakePHP 4 Application?Welcome, today we are ready to create our first CakePHP 4.0 application. Before we create CakePHP 4.0 project we have to do minor modification in the "Php.ini" file. Start your XAMPP control panel and click on “Config” b… Read More
  • How to create controller in CakePHP 4?Hi guys, in our last article we have seen how to setup database in MySQL. It is the first step to start application in CakePHP, or Database First approach. If you have not seen the article visit the link How to setup Database… Read More
  • What is the Design Pattern of CakePHP 4?CakePHP 4.0 follows the MVC (Model-View-Controller) design pattern. MVC consist on three main components. Model, View and Controller Model Any application which is dynamic in nature depend on data, model is the container of… Read More

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.