thinkphp3.0 model (M layer)

Source: Internet
Author: User

Recently learned the PHP framework thinkphp, back to sum up some learning knowledge, I hope to help you

1, the concept of the model:

Model is a very important concept in thinkphp, the rough understanding model is the process of curd operation with database interaction information, and it also includes the definition and instantiation of the model.

2, the model is instantiated:

1, the instantiation of the base model (without formal m-level presence, is thinkphp provides a good, model base class that implements the basic curd)

1, $ object = new Model ("Remove prefix table name")

Quick Create: M

2, $ object = M ("Remove prefix table name")

2, instantiate the custom model (actually/lib/model/by yourself in the project directory)

(1) The naming of models and naming controllers are similar to "table name" +model.class.php

(2) When creating a model class, you must inherit this base class

(3) The model class is itself capable of not defining a method or property

(4) Creating model class objects

1, $ object name = new Xxxmodel ();

Shortcut creation: D The advantage of creating with D is that it is not created every time, and automatically determines if there is an object of that class, it will not be created and the object returned

2, $ object name = D ("XXX");

(5) Instantiating multiple models (instantiating a custom model with the instantiation of another model)

$ Object name = new Xxxmodel ("Name of other model (' User ')");

Note: The database data queried at this time is data from the user table

$ Object name = M ("Xxxmodel:user");

(6) Instantiate an empty model (instantiate the model without knowing it, instantiate an empty model directly)

$ Object name = new Model ();

The object can use the original SQL statement $ object name->query ("native SQL statement");

3, database operation and query (interval query, statistical query, positioning query, native query)

1)

In a coherent operation, a select, such as where group order limit Select, must appear at the last

2)

1, select () return value two-D array equals findall ()

Recommended use of Select ()

2, field query specified fields (one of the consecutive methods)

Field ("username", "password")->select ();

3, find () can only query a record by default is based on the primary key query, there are array conditions, but note that the array condition is a coherent operation of the expression:

If the condition is ID: You can write the condition directly, the default is the ID condition: Find (id=2)

$conditon ["order"] = "id desc";

$conditon ["where"] = "username = ' WJC '";

4, Getbyusername ("admin")

5, GetField (Field 1, Field 2, field 3,spe separator)

Usage:

GetField ($field, $sepa =null)

Parameters:

Field (required): string of fields to get (multiple comma separated)

Sepa (optional): field data interval symbol, if NULL returns an array of arrays. The default is null.

Note: If field is more than one field, return the array. The index of the array is the value of the first field, and the SEPA is null to return the two-dimensional array.

Interval query:

$condition [id]]=array (Array ("GT"), Array ("LT", "," or ");

Or is the associated condition of two queries and is not written by default and

$arr = $user->where ($condition)->select ();

Statistics Query:

"Count the number of user names:". $user->count ("username"). " <br> ";

"Statistics the largest ID:". $user->max ("id"). " <br> ";

"Statistic min ID:". $user->min ("id"). " <br> ";

"Statistic ID's and:". $user->sum ("id"). " <br> ";

Average of the statistic ID:. $user->avg ("id"). " <br> ";

Locate query:

These queries require support from Advanced models: Advmodel

Custom Advanced Model: Class Usermodel extends Advmodel ()

Auto Switch to Advanced Model: Switchmodel (' ADV ')->first ()

Dump ($user->first ());

Dump ($user->last ());

Dump ($user->GETN (index)); 1,2,3,4,5,6

Native query:

The query () method is used for SQL query operations and returns the dataset that matches the query criteria as the Select () method.

The Execute () method is used to update and write the SQL operation of the data (note: Non-query operation, no return dataset), and returns the number of records affected.

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.