The create () method of thinkPHP, but are you sure you want not to add it?

Source: Internet
Author: User
{Code...} I see the example. how can I add an add ()? is there a problem with my settings, or is it necessary to add an add when I create a file?
// Create a new Member data object from the User data object $ User = stdClass (); $ User-> name = 'thinkphp '; $ User-> email = 'thinkphp @ gmail.com '; $ Member = M ("Member"); $ Member-> create ($ User );

Let me look at the example. you have to add ();
Is there a problem with my settings, or do I have to add after creating?

Reply content:
// Create a new Member data object from the User data object $ User = stdClass (); $ User-> name = 'thinkphp '; $ User-> email = 'thinkphp @ gmail.com '; $ Member = M ("Member"); $ Member-> create ($ User );

Let me look at the example. you have to add ();
Is there a problem with my settings, or do I have to add after creating?

Come back for running, answer.
1. the M method source code is as follows, which is used to instantiate a model without a Model file.

/*** Instantiate a Model without a Model file * @ param string $ name Model name. you can specify a basic Model, such as a Model: user * @ param string $ tablePrefix table prefix * @ param mixed $ connection database connection information * @ return Think \ Model */function M ($ name = '', $ tablePrefix = '', $ connection ='') {static $ _ model = array (); if (strpos ($ name, ':') {list ($ class, $ name) = explode (':', $ name);} else {$ class = 'think \ model';} $ guid = (is_array ($ connection )? Implode ('', $ connection): $ connection). $ tablePrefix. $ name. '_'. $ class; if (! Isset ($ _ model [$ guid]) $ _ model [$ guid] = new $ class ($ name, $ tablePrefix, $ connection ); return $ _ model [$ guid];}

2. the source code of create is as follows: it has been commented out. only objects are created, and some filtering and verification operations are performed without being saved to the database.

/*** Create a data object but not save it to the database * @ access public * @ param mixed $ data create data * @ param string $ type status * @ return mixed */public function create ($ data = '', $ type = '') {// if no value is passed, the default value is POST data if (empty ($ data) {$ data = I ('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? :(! Empty ($ data [$ this-> getPk ()])? Self: MODEL_UPDATE: self: MODEL_INSERT); // Check the field ing $ data = $ this-> parseFieldsMap ($ data, 0 ); // check the validity of the submitted field if (isset ($ this-> options ['field']) {// $ this-> field ('field1, field2... ')-> create () $ fields = $ this-> options ['field']; unset ($ this-> options ['field']);} elseif ($ type = self: MODEL_INSERT & isset ($ this-> insertFields) {$ fields = $ this-> insertFields;} elseif ($ type = self:: MODEL_UPDATE & isset ($ t His-> updateFields) {$ fields = $ this-> updateFields;} if (isset ($ fields) {if (is_string ($ fields )) {$ fields = explode (',', $ fields);} // judge the TOKEN verification field if (C ('token _ on ')) $ fields [] = C ('token _ name', null, '_ hash _'); foreach ($ data as $ key => $ val) {if (! In_array ($ key, $ fields) {unset ($ data [$ key]) ;}}// automatically verifies if (! $ This-> autoValidation ($ data, $ type) return false; // form token verification if (! $ This-> autoCheckToken ($ data) {$ this-> error = L ('_ TOKEN_ERROR _'); return false ;} // verify that the generated data object if ($ this-> autoCheckFields) {// when field detection is enabled, the invalid field data is filtered $ fields = $ this-> getDbFields (); foreach ($ data as $ key => $ val) {if (! In_array ($ key, $ fields) {unset ($ data [$ key]);} elseif (MAGIC_QUOTES_GPC & is_string ($ val )) {$ data [$ key] = stripslashes ($ val) ;}}// after creation, the data is automatically processed. $ this-> autoOperation ($ data, $ type ); // assign a value to the current data object $ this-> data = $ data; // return the created data for other calls return $ data ;}

3. source code of the add method: There are several insert methods in it, that is, inserting data into the database.

/*** Add data ** @ access public * @ param mixed $ data * @ param array $ options expression * @ param boolean $ replace whether to replace * @ return mixed */public function add ($ data = '', $ options = array (), $ replace = false) {if (empty ($ data) {// no data is transferred, get the value of the current data object if (! Empty ($ this-> data) {$ data = $ this-> data; // reset data $ this-> data = array ();} else {$ this-> error = L ('_ DATA_TYPE_INVALID _'); return false ;}// data processing $ data = $ this-> _ facade ($ data ); // analysis expression $ options = $ this-> _ parseOptions ($ options); if (false ===$ this-> _ before_insert ($ data, $ options )) {return false;} // write data to the database $ result = $ this-> db-> insert ($ data, $ options, $ replace); if (false! ==$ Result & is_numeric ($ result) {$ pk = $ this-> getPk (); // add a composite primary key to support if (is_array ($ pk )) return $ result; $ insertId = $ this-> getLastInsID (); if ($ insertId) {// Insert ID returned by auto-increment primary key $ data [$ pk] = $ insertId; if (false ===$ this-> _ after_insert ($ data, $ options) {return false;} return $ insertId ;} if (false ===$ this-> _ after_insert ($ data, $ options) {return false ;}} return $ result ;}

Conclusion: The M method instantiates a model class without the model file. The create method only prepares data and is not inserted. The create method inserts data into the database. PS: You can directly look at the source code for this problem.

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.