Home > Backend Development > PHP Tutorial > The simplest way to implement MVC development in PHP - model_PHP tutorial

The simplest way to implement MVC development in PHP - model_PHP tutorial

WBOY
Release: 2016-07-21 15:55:52
Original
764 people have browsed it

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 code The code is as follows:

class module{ 

var $mysql;//Database operation class, which can be mysql, oracle, sql, etc.

var $tbname;//Table name corresponding to the model

var $debug=false;// Is it debugging mode?

function module($tbname,$db=''){}//Constructor function

function _setDebug($debug=true){}//Turn debugging on or off Pattern

function add($row,$tbname=''){}//Add a new record

function query($strsql){}//Direct query sql statement

function count($where='',$tbname=''){ }//Count 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=''){}//Display a record in detail
}
?>


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. 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 in the index.php of the original package

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 registrar. For the principles of using the registrar, you can read the two articles I translated.
Then create a new mysqlController.class.php file with the following code:


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 above is first 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, paging list, multi-table query, etc.




http://www.bkjia.com/PHPjc/318188.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/318188.html

TechArticle

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template