I was taken aback by the use of MVC, which caused a lot of database operations in the program and reduced the performance. 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.
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.
Below is a rough result of MVC model development in PHP. Not the complete code. For the complete code, see the demo package.
- <?
- Class module {
- Var $ mysql; // database operation class,
It 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 = ''){}
- // Counting 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 PHP method for developing MVC models 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, in which the database operation class changed itself to the original Library Class.
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"
,"****","****","52site");
- $phpbean->register('db',$mysql);
- unset($mysql);
- ?>
This php mvc model 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 function, 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.
In the future, I will write a specific demo to illustrate how to use PHP to develop other MVC models, such as queries, updates, additions, paging lists, and multi-Table connections.