ThinkPHP associated models and view Models

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. The view model is more similar to a table virtual table. The view contains a series of columns and row data with names. However, a view does not exist in the database as a stored data value set. Rows and columns are used to define tables referenced by View queries and dynamically generate tables when views are referenced. For the basic table referenced in the table, the view function is similar to filtering. This is the statement in the ThinkPHP manual.
The view model is more similar to a virtual table. The view contains a series of columns and row data with names. However, a view does not exist in the database as a stored data value set. Rows and columns are used to define tables referenced by View queries and dynamically generate tables when views are referenced. For the basic table referenced in the table, the view function is similar to filtering. This is the statement in the ThinkPHP manual.
To put it bluntly, the view model is mysql multi-Table query. The view model does not support multi-Table update or deletion.

The association model is a combination of results after multiple tables are queried. It queries mysql multiple times (after the master table is queried, there will be an operation such as _ after_select, then combine the result set. In some cases, the associated model query is good.
The following are some of my experiences and comparisons on using ThinkPHP's associated models and view models.
I. Performance
View model: join query is used to query multiple tables at a time.
Associate the model, query a table multiple times, and then combine the result set (insert, update, and delete are the same .)
I personally think the performance of the view model will be better (not all of them are, but they have not been tested)
Ii. Query
If it is a query, we strongly recommend that you use the view model. The method is simple and easy to understand and change. The associated model has too many definitions, and the result set is processed and debugged, it is not as convenient as the view model.
3. insert, update, and delete
The view Model is not supported, and the associated Model is supported. However, when inserting or updating a view, the create method of the Model cannot be used, and automatic update is also completed. The automatic update is invalid, I have rewritten A create statement myself. I will give you some tips and you can change it as needed. **
* The create method is overloaded. fields are not filtered and data is generated.
*/
Function create ($ data = '', $ type = ''){
// If no value is passed, the POST data is used by default.
If (empty ($ data )){
$ Data =$ _ POST;
} Elseif (is_object ($ data )){
$ Data = get_object_vars ($ data );
}
// Verify the data
If (empty ($ data) |! Is_array ($ data )){
$ This-> error = L ('_ DATA_TYPE_INVALID _');
Return false;
}
// Status
$ Type = $ type? $ Type :(! Empty ($ data [$ this-> getPk ()])? Self: MODEL_UPDATE: self: MODEL_INSERT );
// Automatic data verification
If (! $ This-> autoValidation ($ data, $ type) return false;
// Form token Verification
If (! $ This-> autoCheckToken ($ data )){
$ This-> error = L ('_ TOKEN_ERROR _');
Return false;
}
// Validate the generated data object
If ($ this-> autoCheckFields) {// if Field Detection is enabled, illegal field data is filtered.
$ Fields = $ this-> getDbFields ();
Foreach ($ data as $ key => $ val ){
If (MAGIC_QUOTES_GPC & is_string ($ val )){
$ Data [$ key] = stripslashes ($ val );
}
}
}
// Automatically process data after creation
$ Data = $ this-> autoOperation ($ data, $ type );
$ Data = $ this-> createData ($ data );
// Return the created data for other calls
Return $ data;
}
 
/**
* Automatic Form Processing
* @ Access public
* @ Param array $ data create data
* @ Param string $ type creation type
* @ Return mixed
*/
Private function autoOperation ($ data, $ type ){
If (! Empty ($ this-> options ['auto']) {
$ _ Auto = $ this-> options ['auto'];
Unset ($ this-> options ['auto']);
} Elseif (! Empty ($ this-> _ auto )){
$ _ Auto = $ this-> _ auto;
}
// Automatic Filling
If (isset ($ _ auto )){
Foreach ($ _ auto as $ auto ){
// Fill factor Definition Format
// Array ('field ', 'fill content', 'fill condition', 'additional rules', [additional parameters])
If (empty ($ auto [2]) $ auto [2] = self: MODEL_INSERT; // It is automatically filled when it is added by default.
If ($ type = $ auto [2] | $ auto [2] = self: MODEL_BOTH ){
Switch (trim ($ auto [3]) {
Case 'function': // use a function to fill the field value as a parameter
Case 'callback': // use the callback Method
$ Args = isset ($ auto [4])? (Array) $ auto [4]: array ();
If (isset ($ data [$ auto [0]) {
Array_unshift ($ args, $ data [$ auto [0]);
}
If ('function' = $ auto [3]) {
$ Data [$ auto [0] = call_user_func_array ($ auto [1], $ args );
} Else {
$ Data [$ auto [0] = call_user_func_array (array (& $ this,
$ Auto [1]
), $ Args );
}
Break;
Case 'field': // fill with values of other fields
$ Data [$ auto [0] = $ data [$ auto [1];
Break;
Case 'ignore': // empty for ignore
If (''= $ data [$ auto [0]) unset ($ data [$ auto [0]);
Break;
 
Case 'string ':
Default: // the string is filled by default.
$ Data [$ auto [0] = $ auto [1];
}
If (false = $ data [$ auto [0]) unset ($ data [$ auto [0]);
}
}
}
Return $ data;
}

/**
* Generate the data required for the associated Model
*/
Function createData ($ data ){
Foreach ($ data as $ k => $ v ){
If (in_array ($ k, $ this-> fields )){
$ Data ['Article'] [$ k] = $ v;
Unset ($ data [$ k]);
}
}
// Delete extra Fields
Unset ($ data ['nid']);
Unset ($ data ['create _ date']);
Return $ data;
}
The original create method is copied directly, but the field filtering is removed and then automatically verified. After the process is completed, the createData method generates the final data. I think the official team should modify this one, that's better.

From: http://www.jishutie.net/topic/350464

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.