After the create method in thinkphp automatically creates the form data, will the fields in the form be saved as attributes of the model object?

Source: Internet
Author: User
Code in the controller: {code...} code in the model: {code...} code in the controller:

If (IS_POST) {if ($ model-> validate ($ model-> _ login_validate)-> create () {if ($ model-> login ()) {if ($ res = session ('returnurl') {session ('returnurl', null); $ returnUrl = $ res ;} else {$ returnUrl = U ('/');} $ this-> success ('login successful! ', $ ReturnUrl); die ;}$ this-> error ($ model-> getError ());}

Code in the model:

Public function login () {var_dump ($ this); die; // Print $ this here and the attributes username and password are not found, but why can we get the value of $ username below ?? $ Username = $ this-> username; $ password = $ this-> password; $ model = M ('member'); $ where ['username'] = $ username; $ user = $ model-> where ($ where)-> find ();...}

Reply content:

Code in the controller:

If (IS_POST) {if ($ model-> validate ($ model-> _ login_validate)-> create () {if ($ model-> login ()) {if ($ res = session ('returnurl') {session ('returnurl', null); $ returnUrl = $ res ;} else {$ returnUrl = U ('/');} $ this-> success ('login successful! ', $ ReturnUrl); die ;}$ this-> error ($ model-> getError ());}

Code in the model:

Public function login () {var_dump ($ this); die; // Print $ this here and the attributes username and password are not found, but why can we get the value of $ username below ?? $ Username = $ this-> username; $ password = $ this-> password; $ model = M ('member'); $ where ['username'] = $ username; $ user = $ model-> where ($ where)-> find ();...}

Is to use get to read non-existing properties in data, and then to data, you can also set the attribute, that is, directly add the attribute after the object.

Public function _ get ($ name) {return isset ($ this-> data [$ name])? $ This-> data [$ name]: null;} public function _ set ($ name, $ value) {// Set data object attributes $ this-> data [$ name] = $ value ;}

Yes. although I have never written this way, I have printed the object after create and found that these attributes exist.

$ Data ['name'] = 'thinkphp'; $ data ['email '] = 'thinkphp @ gmail.com'; $ User-> create ($ data ); // after the data object is created, you can directly read the data echo $ User-> name; echo $ User-> email; // You can also directly modify the created data $ User-> name = 'onethink '; // modify the name field data $ User-> status = 1; // add new field data

Http://www.kancloud.cn/manual...
This section is found in document 3.2.3. $ This-> name you access after you create (); this should be the created attribute
You var_dump ($ model); try ..

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.