Thinkphp Study Notes 1

Source: Internet
Author: User
Tags constant definition

Recently, I learned some basic syntaxes, learned to use the smary template, and then read the thinkphp framework,

This framework is more powerful than I was surprised. Its ror is really strong, and its built-in crud is more intelligent than the Java framework.

I have reproduced some of the following articles to facilitate Memory Learning:

One of curd knowledge, create (create)

Glossary:
1Curd:Four basic operations on the database: Create, update, read, and delete

2Model object:That is, the data object. Each model file in your project corresponds to a data table (or view), and the model and data table have a ing relationship. TP stipulates that the naming rules should follow certain standards. If the naming rules do not match, additional settings should be made as needed. For example, the tablename attribute of the model class

In section 3, I mentioned the naming of model files. Now I will review the content in this section:
We have previously created a think_form database table in the database. In the config. PHP configuration file, we have defined that the data table prefix is think _,
The naming rules for model files are as follows:

Database Table Name that does not contain the prefix and upper-case letters + model. Class. php

Therefore, we used to create a file formmodel. Class. php In the MyApp/lib/model directory for the think_form data table.

Special names of model classes can also be used to intelligently identify the names of camper tables. Suppose we have a table like think_new_table.
You can name it newtablemodel. Class. php. By default, You can intelligently identify automatically corresponding think_new_table tables, so you do not have to modify the configuration.

During the development process, you can simply define the model class (xxxmodel. Class. php) corresponding to the data table to perform data operations.
How to enable this model to support automatic verification, automatic filling, and automatic filtering of this knowledge will be explained in the following sections.

In general, adding data to a table is created through forms. To learn more about C in curd, we will create a form to add data.
Previously, we have created the formmodel. Class. php class ---- model (m) under MyApp/lib/model)
At the same time, I added an index application module under MyApp/lib/Action/and wrote down an index operation ---- controller (c)
In addition, the corresponding template directory indexand index.html template file ------- view (v) is created under the tpl)
The development of the MVC model has been reflected.
Tdweb briefly summarized the relationship between the three in TP,

C is used to obtain data from m to v,

Using TP to submit data to the database using forms. The process is no different from what you know about form submission, but TP simplifies the process of data operations.
Let's take a look at the actual operation.
First, let's take a look at the fields of the form table we have defined:

  1. 'Id', // automatic ID
  2. 'Title', // Title
  3. 'Content', // content
  4. 'Create _ time', // Creation Time
  5. 'Update _ time' // Update Time
  6. 'Email ', // email
  7. 'Status', // status

Copy code

The create_time field is used to record the data insertion time. We can use the automatic filling of TP data for processing.

The $ _ auto attribute is defined in the model class, which can be used to process the default value and other fields written by the system.
Note 1: This automatic filling may overwrite the form submission project. The purpose is to prevent the form from submitting fields illegally.
NOTE 2: Table data processing is automatically performed only when the create method of the model class is used to create a data object.

1 open the MyApp/lib/model/formmodel. Class. php file and change the code

  1. <? PHP
  2. Class formmodel extends model {
  3. // Automatic Filling settings
  4. Protected $ _ auto = array (
  5. Array ('status', '1', 'add '),
  6. Array ('create _ time', 'time', 'add', 'function '),
  7. );
  8. }
  9. ?>

Key Points of code knowledge:
The $ _ auto attribute of the model class is an array composed of multiple fill factors. the fill factor Definition Format is as follows:

Array (fill field, fill content, fill condition, additional rules)

Fill Field: It is the form field to be processed. This field is not necessarily a field in the database table. It can also be processed for auxiliary detection fields in the form, such as repeated passwords and verification codes.
Filling Condition: Add | update | all (Note: This is version 1.5 description. version 1.6 has been changed to 1 2 3, which will be explained later)
When the value is add, the data is automatically filled when new data is added. This is the default processing method.
When the value is update, the data is automatically filled during data update.
When it is set to all, auto-fill will be performed in all cases.

Additional rules:The additional rules are for filling the content, including function, callback, field, and string.
It is easy to understand that fields are filled with other fields and strings are directly marked as strings as values. For example

  1. Array ('status', '1', 'add '),

Copy code

That is, the value of the Status field is directly filled with 1.

The following describes two additional rules: function and callback.

  1. Protected $ _ auto = array (
  2. Array ('create _ time', 'time', 'add', 'function '),
  3. );

The above create_time indicates the field to be processed. The filled content is time, the additional rule is that the function is used by the function, and the filling condition is processing when the add operation is added, the whole line of code indicates that the time function is automatically filled as the value of the create_time field when it is added.
Let's look at another example of using a function as an additional rule.

  1. Protected $ _ auto = array (
  2. Array ('Password', 'md5', 'add', 'function '),
  3. );

When function is used as an additional rule, the second filling content indicates that this factor is a function name. The parameter of this function indicates the value of the filled field. For example, the value of password is 123456, the above code will first use the MD5 function ('20140901') to process the value and then insert it into the data table.
The function and callback theories are the same, but one is to indicate that the filled content is written by the function name, and the other is to indicate that the filled content is the method name in the class. A function can be a PhP5 built-in function or a function written by yourself. Callback is a method that can be called by the current model class.
For more information about data filling, see the official user manual thinkphp data operation guide.

2 open the MyApp/TPL/default/index/index.html file, and write the form into the modified Code.

  1. <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
  2. <HTML>
  3. <Head>
  4. <Title >{$ title }</title>
  5. </Head>
  6. <Body>
  7. <Form action = "_ URL _/Add" method = "Post" name = "formname" id = "formname">
  8. <P>
  9. <Label for = "title"> title: </label>
  10. <Input name = "title" type = "text" id = "title"/>
  11. </P>
  12. <P>
  13. <Label for = "email"> Email: </label>
  14. <Input name = "email" type = "text" id = "email"/>
  15. </P>
  16. <P> <label for = "content"> content: </label> </P>
  17. <P>
  18. <Textarea name = "content" rows = "5" Cols = "25" id = "content" class = "textarea"> </textarea>
  19. </P>
  20. <P> <input type = "Submit" value = "Submit"/> </P>
  21. </Form>
    </Body>
    </Html>

In the code above, we have simply created a form and used a template variable {$ title} for the title }.
The processing address submitted by the action is _ URL _/Add, where _ URL _ is a constant definition, indicating the address of the current module, the TP template engine automatically interprets this statement as/index. PHP/index/Add. Common templates use constants:

_ Root _ website root directory address
_ App _ current project (entry file) Address
_ URL _ current module address
_ Action _ current operation address
_ Self _ current URL

3. Set the template variable {$ title} and add the add operation method.

Open the MyApp/lib/Action/indexaction. Class. php file and modify the Code as follows:

  1. <? PHP
  2. Class indexaction extends action {
  3. Public Function Index (){
  4. $ This-> assign ('title', 'add data'); // The template variable {$ title} is set here}
  5. $ This-> display ();
  6. }
  7. // Method for processing form data
  8. Function add (){
  9. $ Form = D ("form ");
  10. If ($ form-> Create ()){
  11. $ Form-> Add ();
  12. $ This-> redirect ();
  13. } Else {
  14. Header ("Content-Type: text/html; charset = UTF-8 ");
  15. Exit ($ form-> geterror (). '[<a href = "javascript: history. Back ()"> return </a>]');
  16. }
  17. }
  18. } // Class definition end
  19. ?>

Key Points of code knowledge:
The previous chapter on Template variable assignment has been introduced. Here we will not be embarrassed.

Let's take a look at the code of the add method.
$ Form = D ("form ");
The unique single-letter function D of TP is used here. It is the most commonly used TP function for database operations, indicating the instantiation of form objects,
That is, $ form = new formmodel ();
For specific code of the D function, you can view the fragments in common/function. php In the thinkphp directory,
It automatically introduces the model class and determines that if the model has been instantiated before, it will not be instantiated. If the model does not exist, an exception error will be thrown. In addition, D can access the model across projects, ignore.

$ Form-> Create ()
Use the create method of the model class to create a form object. If the object fails, false is returned.

$ Form-> Add ();//The add method writes form data.
$ This-> redirect ();// Execute jump
Just a few lines of code can complete the data insertion process.

Related Article

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.