Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Thinkphp Note 2

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Controller creation principles:

Basic principle: according to the function, in principle, a function item corresponds to an action in the controller.

It is possible that multiple functions share an action that is close to adding and modifying. Choose whether to add or modify according to the conditions.

An auxiliary method may be needed to complete an action

Basic principle: divide the controller class according to the data table structure according to the function

According to the basic principle, the project contains the functions of several different user roles. If you do not distinguish between modules, it may lead to controller renaming, affect code reading, and divide different modules according to different role functions.

Is it necessary to create a module

How to classify controller files

How to make the controller action

two。 The erection of view files

Static resource file

Composition: picture css JavaScript

File directory: public/ subdirectory

Layout file

Composition: page head, tail, bread crumbs, body

Directory: module directory / view/

View file

General principle: a method in the controller corresponds to a view file

3. Basic use of database model

Connect to the database

Use Profil

Input parameter form

Instantiate database operation objects

Create using new Model ()

Create using the M () function

Execute SQL native query

Query () method

Test results of Dump () function

4. Basic operation of database

String query

$table = M ('tableName')

$results = $table-> where ('id > 3')-> select ()

Associative array query

$condition ['id'] = array (' neq','3')

$condition ['uid'] = array (' neq','1')

$results = $table-> where ($conditon)-> select ()

Magic method query

$results = $table-> getById ('1'); / / only the first record of the specified condition can be obtained

Statistical data

$count = $table-> count (); / / data in statistical tables

$count = $table-> max ('id')

Limit

$limit = $table-> limit (3)-> select (); / / first three records

$limit = $table-> limit (2jue 2)-> select (); / / the third record starts to query two

Order sorting

$order = $table-> order ('id desc')-> select (); / / id in descending order

Create and insert data

Create (): creates data in memory, does not write to the database, and filters out invalid fields (fields that are not in the table)

Workflow: 1. Get the data (POST parameter or data passed in by the user) 2. Perform data check 3. Put the incoming data in the data property of the current object

Parameter: 1. If there is no parameter, insert $_ POST [] data 2. You can pass in associative array data 3. Standard object data can be passed in

Model::MODEL_INSERT Model::MODELUPDATE

Return value: created data object group

You can add or modify properties directly to the Model object for the purpose of adding in-memory data

Data (): basically the same as create (), without data verification

Add (): inserts data in memory into a table

1 without parameters, directly use the data property of the MODEL object (returned by the create () and data () methods)

2 additional conditions

3 whether to overwrite the identity (whether to overwrite the existing primary key data)

4 you can directly filter out the fields that do not exist in the data table.

5 use add () directly without data verification

/ / check before writing data

If ($this-> create ($data))

{

Return $this- > add ()

}

Read data

Select (): 1 does not pass all records in the query table 2. Pass a parameter select (1) A record with a primary key of 1 3.select (1) returns a record with a primary key of 135 4 pass in false to return the statement of the current query

Find (): query a record, get the first record without passing a parameter, and find (1) returns a record with a primary key of 1

GetField (): pass in only one parameter to get the value of the first record

Where condition: 1 pass string 2 into an array or object to filter out illegal characters.

Limit (): limit (1) 1. Get 1 record of the specified condition limit (2) get 2 records starting from the first parameter

Order (): sort records by specified field, single-field sort and multi-field sort

Delete data: the number of deleted data is returned for successful deletion, and false is returned for failure.

$db = M ('msgs')

$results = $db-> where ('id = 1')-> delete (); / / Delete the record with id 1

$results = $db-> where ('1')-> delete (); / / Delete all records

$results = $db-> delete (); / / return false without passing parameters

$results = $db-> delete ('1Jing 3Jing 5') / / Delete a record with a primary key of 135

Modify data: update data for specified conditions

$data ['name'] = "Zhang San"

$results = $db-> where ('id = 1')-> save ($data)

If the parameter is passed, it indicates the effect to be satisfied after the update.

If no parameter is passed, the record in the data attribute of the object is manipulated using the current data table.

Update condition, you can use the where display to indicate the update condition, if there is no update condition, the record in the save () parameter contains the primary key, update the record corresponding to the primary key, otherwise it will not be updated. The number of updated records is returned if the update is successful. If the update fails, false is returned.

SetField: update the values of individual fields

SetInc plus setDec minus

5. Model operation class

Function: MVC is relatively thoroughly separated, clear logic, suitable for division of labor and cooperation, easy to achieve the effect of some features (filtering, verification, automatic completion)

Create a model class:

File name: Datasheet name Model.class.php

Directory: module / model/ directory

Class name: class Datasheet name Model extends Model

Optional attribute: tablePrefixtableName trueTableName dbName _ validate (automatic validation rule) _ auto (autocomplete rule)

Add methods to implement business logic

Use: new method D () function

6.ActivRecord

Tables map to classes, records map to objects

ORM model: object-relational mapping model

7.session processing

Initialization: no manual call is required, unless there is a special need

Setting value: session (key, value)

Obtained value: session (key)

Delete value: session (key, null)

Determine whether the value exists: session (? Key)

Use session in the template: judge the session status output session value

Output session dump ($_ SESSION) from the controller

The present tag is used to determine whether a variable has been defined

Welcome, {$Think.session.loginedUser}! | Log out you have not logged in (login) and do not have a user name (registration) 8. Paged page jump

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report