Correct understanding of PHP Development MVC Model _php Tutorial

Source: Internet
Author: User

I was surprised to get a lot more database operation using MVC, which makes performance drop. MVC is just a framework that has no relation to database operations. MVC just provides a clear pattern of programming development, and as long as you handle it well, it is impossible to do many unnecessary database operations. An MVC is definitely not a good MVC architecture if it allows a programmer to do a lot more database operations without knowing it. I think MVC just provides a simple development framework, it is not necessary to integrate many library classes, library classes are best to let the programmer choose to use.

I did not delve into the theory of MVC, for me personally, the model is a database encapsulation, the method of invoking the model, you can get the corresponding data, but the implementation of the details of the programmer does not need to care. In actual development, it is likely that a database table corresponds to a model.

For example, a user information table userinfo, corresponding to a model user, by invoking the model user's Add () method you can add a data to the database, through select () you can implement the query, update can be implemented by updating. The model should also be independent of the specific database type, whether you are using mysql,oracle or SQL Server.

At the same time, I do not recommend the use of ROR in web development, complex multi-table query using SQL language is how convenient and fast things, and better performance. If a programmer doesn't have the knowledge of SQL, I don't think he is a qualified programmer. So, in my model, I provide a query method to implement a direct SQL query.

The following is a general result of the PHP development MVC model. Not complete code, please see Demo package for complete code.

 
 
  1. < ?
  2. Class module{
  3. var $mysql;//Database Operation class,
    It could be mysql,oracle,sql and so on.
  4. var $tbname;//table name corresponding to the model
  5. var $ Debug = false ;//Whether it is debug mode
  6. function Module ($tbname, $db=') {}
    constructor function
  7. function _setdebug ($debug=true) {}
  8. Turn debug mode on or off
  9. function Add ($row, $tbname=") {}
  10. Add a new record
  11. function query ($strsql) {}//query SQL statement directly
  12. function count ($where=', $tbname=' ){ }
  13. Count statistics
  14. function Select ($where=', $tbname= '' ){}
  15. Inquire
  16. function Delete ($where=', $tbname= '' ){}
  17. Delete a record that satisfies a condition
  18. function Update ($set, $where, $tbname=') {}
  19. Update a specified record
  20. function Detail ($where, $tbname=") {}
  21. Show a record in detail
  22. }
  23. ?>

In this model, I use arrays and database fields to correspond. The early Phpbean used objects to correspond. But then it felt that PHP's approach to developing the MVC model was not good in PHP, and added a lot of meaningless classes. Using arrays is more convenient and works better (arrays in PHP are really good things, which is a lot better than Java).

In the demo below, I used the MySQL database to demonstrate that the database operation class changed itself to one of the original library classes.

Below, the detailed explanation uses the demo. ^_^
Added to the index.php of the original package

 
  
  
  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 development MVC Model Code is mainly to register MySQL into the registrar, about the use of the principle of the Registrar, you can see my translation of the two articles.
Then create a new mysqlController.class.php file with the following code:

 
 
  1. < ?
  2. /**
  3. * MVC Demo Demo
  4. * Only the most basic MVC functionality is implemented and does not include secure handling
    , 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 is the table name
  15. $this- > module- > query ("Set names ' gb2312 '");
    If it's MYSQL5, please add this.
  16. }
  17. function Indexaction () {
  18. Print_r ($this->module->Select ());//This enables reading of data
  19. }
  20. }
  21. ?>

Above first is the Controller's constructor, adding a model. It then invokes the model's method in the Indexaction to display the data. This allows the simplest query list to be implemented.

Later I will write a specific demo to illustrate how to use PHP to develop the MVC model of other methods, such as query, update, add, page list, multi-table connection and so on.


http://www.bkjia.com/PHPjc/446086.html www.bkjia.com true http://www.bkjia.com/PHPjc/446086.html techarticle I was surprised to get a lot more database operation using MVC, which makes performance drop. MVC is just a framework that has no relation to database operations. MVC just provides a kind of ...

  • 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.