Home Backend Development PHP Tutorial The simplest way to implement MVC development in PHP - model

The simplest way to implement MVC development in PHP - model

Jul 29, 2016 am 08:36 AM
function module mvc mysql nbsp

Yesterday, someone in the group said that using MVC adds a lot of database operations to the program, which reduces performance. This really surprised me. MVC is just a framework and has nothing to do with database operations. MVC only provides a clear programming development model. As long as you handle it well, it is impossible to do a lot of unnecessary database operations. If an MVC allows a programmer to perform many more database operations without knowing it, it is definitely not a good MVC architecture. I think MVC only needs to provide a simple development framework. There is no need to integrate many library classes. It is best for programmers to choose to use library classes.
The purpose of my own MVC framework is just to implement a simple MVC process. Others can be added in detail according to your specific situation. Be truly compact, flexible and efficient!
I wrote two articles in the past few weeks, "The easiest way to implement MVC development in PHP - view and template technology", "The easiest way to implement MVC development in PHP - single point of entry". Today I will talk specifically about how to implement the MVC model.
I have not studied the theory of MVC in depth. For me personally, the model is a database encapsulation. By calling the model method, you can get the corresponding data, but the programmer does not need to care about the implementation details. In actual development, it is likely that a database table corresponds to a model. For example, a user information table userinfo corresponds to a model user. By calling the add() method of the model user, you can add a piece of data to the database, you can query it through select(), and you can update it through update. At the same time, the model should be independent of the specific database type, whether you use mysql, oracle or sql server. At the same time, I do not recommend using ROR in WEB development. It is so convenient and fast to use SQL language for complex multi-table queries, and the performance is better. If a programmer doesn't even have knowledge of SQL, I don't think he is a qualified programmer. Therefore, I provide a query method in my model to implement direct SQL query.
The following is an approximate result of the model. This is not the complete code. Please see the demo package for the complete code.

Copy the code The code is as follows:


class module{
var $mysql;//Database operation class, which can be mysql, oracle, sql, etc.
var $tbname;//Model correspondence Table name
var $debug=false;//Whether it is debug mode
function module($tbname,$db=''){}//Constructor
function _setDebug($debug=true){}//Turn on or Turn off debugging mode
function add($row,$tbname=''){}//Add a new record
function query($strsql){}//Query the sql statement directly
function count($where='',$ tbname=''){ }//Counting statistics
function select($where='',$tbname=''){}//Query
function delete($where='',$tbname=''){} //Delete a record that meets the conditions
function update($set,$where,$tbname=''){}//Update the specified record
function detail($where,$tbname=''){}//Detailed display A record
}
?>


In this model, I use arrays and database fields to correspond. In the early PHPBEAN, objects were used to correspond. But later I felt that this method was not good in PHP and added a lot of unnecessary classes. It is more convenient and better to use arrays (arrays in PHP are indeed a good thing, much better than JAVA).
In the demo below, I used the mysql database for demonstration, in which the database operation class was changed to one of my original library classes. For details, please see "Modify the previous library class, php5->php4".
Next, the use of demo will be explained in detail. ^_^
Add
require_once(SITE_PATH.'/libs/phpbean.class.php');
require_once(SITE_PATH.'/libs/mysql.class.php ');
$phpbean=new phpbean();
global $phpbean;
$mysql=new mysql("localhost","****","****","52site");
$phpbean ->register('db',$mysql);
unset($mysql);
?>
This code mainly registers MYSQL into the register. For the principle of using the register, you can read my translation of two articles.
Then create a new mysqlController.class.php file with the following code:
/**​
​ * MVC demonstration demo ​
​ * Only implements the most basic MVC functions, and does not include security processing, data filtering, and other optimization measures.
* @author:feifengxlq
* @since:2007-1-24
* @copyright http://www.phpobject.net/blog/
*/
class mysqlController
{
var $module;
function mysqlController(){
                 require_once(SITE_PATH.'/ libs/module.class.php');
$this->module=new module('52site_siteinfo');//52site_siteinfo is the table name
$this->module->query("set names 'gb2312' ");//If it is MYSQL5, please add this sentence
}
function indexAction(){
      print_r($this->module->select());//In this way, reading data is achieved                                                                              
?>
The first thing above is to add a model to the constructor of the controller. Then call the model method in indexAction to display the data. This implements the simplest query list. You can check your results through this address http://path/to/yoursite/mv...
In the future I will write a specific demo to explain how to use other methods of the model, such as query, update, add, Paginated lists, multi-table query, etc.

The above has introduced the simplest way to implement MVC development in PHP - the model, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles