A Sample Web Application with the HMVC Design Pattern - Building model classes for database records
(Page 4 of 4 )
Since this sample framework has already been packaged with a generic model capable of performing CRUD operations on a selected MySQL table, defining the models that will fetch data from the previous "links" and "news" tables is only a matter of deriving a pair of subclasses from the corresponding parent model and nothing else.
In short, the files containing the definitions of these brand new model classes will be as follows:
(LinkModel.php)
<?php
// define LinkModel class as subclass of Model
class LinkModel extends Model{}
// End LinkModel class
(NewsModel.php)
<?php
// define NewsModel class as subclass of Model
class NewsModel extends Model{}
// End NewsModel class
Short to code and read, isn't it? As I explained a moment ago, each of the above files includes a subclass that inherits all of its functionality from the parent model. In this specific case, the classes were named "LinkModel" and "NewsModel," as they're going to be bound to the tables that store link and news-related data, but logically you may want to name them something else.
Finally, it's valid to mention that extending the generic model is an entirely optional process, since it's possible to work equally with multiple instances of it. Nonetheless, I decided to create two separate models to keep the application's code more readable and potentially easier to scale.
At this point, the sample program being developed here is starting to look slightly more functional, and most importantly, it sets the appropriate structure for implementing a working HMVC layer. Achieving this is a bit premature, however, so for the moment take a little time to read the following conclusions.
Final thoughts
That's all for the moment. In this seventh chapter of the series, I started building an example MySQL-driven application, whose main task will consist of displaying a simple web page with data fetched from two different database tables.
To be frank, in its current state the application does nothing particularly useful, as it's only composed of the corresponding MySQL tables that make up the data layer and the model classes that interact with them independently. As I said before, however, this is work in progress; in subsequent tutorials the program will be given the ability to function through an HMVC layer.
In the next part I'm going to add to the program the controllers bound to the models that you learned before. Therefore, now that you've been warned about the topics that will be covered in the forthcoming article, you won't want to miss it!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.