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.
- < ?
- Class module{
- var $mysql;//Database Operation class,
It could be mysql,oracle,sql and so on.
- var $tbname;//table name corresponding to the model
- var $ Debug = false ;//Whether it is debug mode
- function Module ($tbname, $db=') {}
constructor function
- function _setdebug ($debug=true) {}
- Turn debug mode on or off
- function Add ($row, $tbname=") {}
- Add a new record
- function query ($strsql) {}//query SQL statement directly
- function count ($where=', $tbname=' ){ }
- Count statistics
- function Select ($where=', $tbname= '' ){}
- Inquire
- function Delete ($where=', $tbname= '' ){}
- Delete a record that satisfies a condition
- function Update ($set, $where, $tbname=') {}
- Update a specified record
- function Detail ($where, $tbname=") {}
- Show a record in detail
- }
- ?>
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
- < ?
- 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 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:
- < ?
- /**
- * MVC Demo Demo
- * Only the most basic MVC functionality is implemented and does not include secure handling
, 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 is the table name
- $this- > module- > query ("Set names ' gb2312 '");
If it's MYSQL5, please add this.
- }
- function Indexaction () {
- Print_r ($this->module->Select ());//This enables reading of data
- }
- }
- ?>
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 ...