Correct understanding of php mvc model development

Source: Internet
Author: User

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.

 
 
  1. <?
  2. Class module {
  3. Var $ mysql; // database operation class,
    It can be mysql, oracle, SQL, etc.
  4. Var $ tbname; // table name corresponding to the model
  5. Var $ debug = false; // whether the debug mode is used
  6. Function module ($ tbname, $ db = ''){}
    // Constructor
  7. Function _ setDebug ($ debug = true ){}
  8. // Enable or disable the debug mode
  9. Function add ($ row, $ tbname = ''){}
  10. // Add a new record
  11. Function query ($ strsql) {}// directly query SQL statements
  12. Function count ($ where = '', $ tbname = ''){}
  13. // Counting statistics
  14. Function select ($ where = '', $ tbname = ''){}
  15. // Query
  16. Function delete ($ where = '', $ tbname = ''){}
  17. // Delete a record that meets the conditions
  18. Function update ($ set, $ where, $ tbname = ''){}
  19. // Update a specified record
  20. Function detail ($ where, $ tbname = ''){}
  21. // Display a record in detail
  22. }
  23. ?>

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

 
 
  1. < ?   
  2. require_once(SITE_PATH.'/
    libs/phpbean.class.php');   
  3. require_once(SITE_PATH.'/
    libs/mysql.class.php');   
  4. $phpbean=new phpbean();   
  5. global $phpbean;   
  6. $mysql=new mysql("localhost"
    ,"****","****","52site");   
  7. $phpbean->register('db',$mysql);   
  8. unset($mysql);   
  9. ?>  

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:

 
 
  1. <?
  2. /**
  3. * MVC demo
  4. * Only implement the most basic MVC function, excluding Security Processing
    , Data filtering, and other optimization measures.
  5. * @ Author: feifengxlq
  6. * @ Since: 2007-1-24
  7. * @ Copyright http://www.phpobject.net/blog/
  8. */
  9. Class mysqlController
  10. {
  11. Var $ module;
  12. Function mysqlController (){
  13. Require_once (SITE_PATH. '/libs/module. class. php ');
  14. $ This-> module = new module ('52site _ siteinfo ');
    // 52site_siteinfo indicates the table name.
  15. $ This-> module-> query ("set names 'gb2312 '");
    // Add this sentence if MYSQL5 is used.
  16. }
  17. Function indexAction (){
  18. Print_r ($ this-> module-> select (); // read data.
  19. }
  20. }
  21. ?>

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.


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.