PHP: The simplest way to implement MVC development -- Model _ PHP Tutorial

Source: Internet
Author: User
PHP implements the simplest method of MVC development-model. Yesterday, some people in the group said that the use of MVC allowed the program to operate a lot more databases, reducing the performance, which really surprised me. MVC is just a framework and has nothing to do with database operations. Yesterday, some people in the group said that the use of MVC allowed the program to operate a lot more databases, reducing the performance, which really surprised me. MVC is just a framework and has nothing to do with database operations. MVC only provides a clear programming and development mode. as long as you handle well, you cannot perform many unnecessary database operations. If an MVC allows a programmer to perform a lot of database operations without knowing it, it is definitely not a good MVC architecture. I think MVC only needs to provide a simple development framework, and there is no need to integrate many library classes. it is best to allow programmers to choose their own library classes.
The purpose of my own MVC framework is just to implement a simple MVC process. other users can add them as needed. Truly small, flexible, and efficient!
I wrote two articles in the last few weeks, PHP: The simplest way to implement MVC-View and template technology, and PHP: The simplest way to implement MVC-single point of entry. Today we will talk about how to implement the MVC model.
I have not studied the MVC theory in depth. for me personally, the model is a database encapsulation. you can obtain the corresponding data by calling the model method, but the implementation details do not need to be concerned by programmers. In actual development, it is very 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 data record to the database by using select () you can implement query and update through update. At the same time, the model should be unrelated to 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 very convenient and fast to use the SQL language for complex multi-table queries, and the performance is better. If a programmer has no 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 queries.
The following is a rough result of the model. Not the complete code. for the complete code, see the demo package.

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; // whether the debug mode is used

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

Function _ setDebug ($ debug = true) {}// enable or disable the debug mode

Function add ($ row, $ tbname = '') {} // add a new record.

Function query ($ strsql) {}// directly query SQL statements

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 a specified record

Function detail ($ where, $ tbname = '') {}// display a record in detail
}
?>



In this model, I use arrays and database fields to correspond. Objects are used in early phpbeans. But later I felt that this method was not good in PHP and added a lot of unnecessary classes. It is more convenient to use arrays and better results (arrays in PHP are indeed a good thing, which is much better than JAVA ).

In the demo below, I used the mysql database for demonstration. the database operation class changes itself to an original database class. for details, see "modify previous database classes, php5-> php4.

The demo is described in detail below. Pai_^
In the index. php of the original package, 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", "*****", "*****", "52 site ");
$ Phpbean-> register ('DB', $ mysql );
Unset ($ mysql );
?>

This code is mainly used to register MYSQL into the register. for the usage principle of the register, see the two articles I have translated.
Create a new mysqlController. class. php file with the following code:

/**
* MVC demo
* Only implement the most basic MVC functions, excluding 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 indicates the table name.
$ This-> module-> query ("set names 'gb2312 '"); // add this sentence if MYSQL5 is used.
}

Function indexAction (){
Print_r ($ this-> module-> select (); // read data.
}
}
?>

First, add a model to the controller constructor. Then, call the model method in indexAction to display the data. In this way, the simplest query list is implemented. You can view your results through this address: http: // path/to/yoursite/mv...
In the future, I will write a specific demo to illustrate how to use other methods of the model, such as query, update, add, paging list, multi-table join query, and so on.

Bytes. MVC is just a framework and has nothing to do with database operations ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.