This time the main content is the basic operation of the model
0x01: What is a model
Through the manual reading, generally speaking, the opening of the database and other operations in another PHP file
As well as the rules of the variable specific details, query, value and other operations to define, convenient for direct use in the controller.
0X02: Use of models
Create a directory with controller siblings in the index directory model
Create a PHP model file and call the Think\model file
<?php namespace App\index\model; UseThink\model; classUserextendsmodel{protected $table= ' Think_user '; protected functionGetbirthdayattr ($birthday){ return Date(' y-m-d ',$birthday); } protected functionSetbirthdayattr ($value){ return Strtotime($value); } protected functionScopeemail ($query){ $query->where (' email ', ' [email protected] '); } }?>
Table is the GET database variable that was previously associated with the Open database in the application\database.php file.
Naming rules in getbirthdayattr functions get + name + Attr
This allows you to invoke the Echo birthday variable directly in the controller without having to convert it with the date () function.
setbirthdayattr function named rule set + name + Attr
This modifies the birthday in the controller without calling the Strtotime () function, but directly user->birthday = ' xxxx-xx-xx '
Naming rules in the Scopeemail function scope + Name
This allows you to query the associated content without having to write SQL statements or thinkphp SQL calls.
Note: the "name" in the function here will cause an error, even if the variable name is not the variable name.
0X03: The invocation of the model in the controller
The operation of the database is not intended to be deleted and modified
Public functionAdd () {$user=NewUsermodel; $user->nickname = ' Sijidou '; $user->email = ' [email protected] '; $user->birthday = "1998-10-30"//Strtotime(' 1998-10-30 '); if($user-Save ()) { returnincrease success information; } Else { return $user-GetError (); } }
The increase is undoubtedly a new model, note that the object here is the name of the class inside the model, because the controller name is also called user to modify the user name in the model under the reference.
Use as Usermodel;
The birthday here actually calls the SETBIRTHDAYATTR function in the model, and if there is no setbirthdayattr function, the information can be stored, but the time fixed at the last output is the default value of 1970-01-01 (I am here)
Public functionUpdate$id){ $user= Usermodel::get ($id); $user->nickname = ' Tudou '; $user->email = ' [email protected] '; $user->birthday = ' 1999-4-5 '; $user-Save (); } Public functionDelete$id){ $user= Usermodel::get ($id); if($user){ $user-Delete (); return' Delete User Success '; } Else{ return' Delete User not successful '; } } Public functionSearch$email){ $list= Usermodel::scope (' email ',$email),Select (); foreach($list as $user){ Echo $user->nickname. ' <br/> '; Echo $user->email. ' <br/> '; Echo $user->birthday. ' <br/> '; } }
In the case of the change and deletion, the GETBIRTHDAYATTR function is called by default, and the scope in the check is called the Scopeemail function. If the email variable is nickname and the corresponding Scopenickname ($nickname) is added to the model, then the query can be based on the value of nickname.
0X04: Interaction with view
Here I use the official HTML template, the source code is not posted, anyway is a form table, and a little CSS.
Here to upload the contents of the controller into the view, you need to use the view method, and the corresponding open view file name default to the method name
The path to access is Http://192.168.60.132/index/user/create
The Add () function is modified here
The input function is a unique parameter of thinkphp, and the main function is to modify the contents of the code to replace it with the "post" that receives the post reference. In the '. ' is mainly used to match all parameters, if specific to one can be "post.nickname"
Allowfield (TRUE) is a function that is unique in thinkphp, which is used to filter data in non-data tables
The Validate () function is a function that is unique to tinkphp, and is primarily used to verify that the data that is post is compliant.
The contents of validate are defined in the Validate template (not defined in version 5.1, just write a function directly under the controller)
Validate template path. \index\validate\user.php
Content is
Rule to give key value pairs
Require is required
Min:5 is a minimum length of 5
Email is the detection of compliance with the mailbox specification
DATEFORMAT:Y-M-D Check compliance with date specification (value is not within the month)
Checkmail Custom Method: $value + e-mail parameter $rule = ' qq.com ' rule, use if statement to determine
Preg_match is a comparison function for regular expression matching
0x05: Correlation and output
The manual speaks a lot of, nothing more than a pair of one or one-to-many, many-to-many
One-to: Use This->hasone (another model name), another with Belongsto (the previous model name)
One-to-many: define additional () in a this->hasmany (), and the others
Many-to-many: as much as a pair, that is, in each model of the Belongtomany (corresponding to model 1, corresponding to Model 2, ...) )
In Add if you want to modify the associated model
association model variable = content of current model, association model
Save time with (the example function here, the author lazy do not want to test, later used to use)
Model output
Is nothing more than the operator inside the output or jump to the view inside the output
Jump to view (' View file name '), typically in a view file in the controller's sibling directory
The default parameter is the same as the method name.
Output with Var_dump in operator, return is OK
It is also possible to use ToArray () to output the array, hidden ([variable name]) to hide the corresponding variable output, and visible ([variable]) to specify the output of the variable with only the definition.
Summary: This section of the content is almost like this, I wrote a few examples I think too detailed, so much like textbook Journal, write blog mainly notes, remember the focus, so here is more concise. And the final relevance of the relevant content is not tested, wait until the actual operation time to use it. Recently to the end of the semester, and no final review, and did not write a blog, it is too decadent.
First acquaintance thinkphp (5)