ThinkPHPThink location query

Source: Internet
Author: User
The first () method is used to obtain the first record that meets the condition. it is a method in the advanced model class. you can refer to getN () to locate the query.

ThinkPHP supports data locating and query:

GetN (): Gets a record at a certain position in the query result.
First (): obtain the first qualified record
Last (): obtain the last record that meets the condition

Location query is a built-in method in the advanced model class (AdvModel). It is an independent method and supports consistent operations. If M or D is used directly for instantiation

The model prompts:

Model: getN the method you requested does not exist!

Therefore, to use the positioning query, you need to load the advanced model class. There are two ways to load the advanced model class:

1. use dynamic model switching
SwitchModel ()
Method:

SwitchModel ("Adv ")

2. inherit the advanced model class from the user model:

Class UserModel
Extends AdvModel {}

GetN ()
SwitchModel ()

The following describes how to use switchModel ()
Method to locate the Query example:




  1. Public function read (){
  2. $ Dao = M ('user ');
  3. $ List = $ Dao-> switchModel ('adv ')-> where ('uid> 0')-> order ('uid ASC')-> getN (2 );
  4. Dump ($ list );
  5. }

In the preceding example, uid> 0 is obtained.
3rd records. The actual executed SQL statement is as follows:




  1. SELECT * FROM user WHERE uid> 0 order by uid asc limit 2, 1

Therefore, the parameter in getN () is LIMIT.
The first parameter (cursor offset) in ).
AdvModel

In Lib/Model/UserModel. class. php
The file defines the model as follows:




  1. Class UserModel extends AdvModel {
  2. }
  3. ?>

Since model classes do not need to implement any logic, it can be an empty model. The controller code is as follows:




  1. Public function read (){
  2. $ Dao = D ('user ');
  3. $ List = $ Dao-> where ('uid> 0')-> order ('uid ASC ')-> getN (2 );
  4. Dump ($ list );
  5. }

Execution result and use switchModel ()
.
First ()

The first () method is used to obtain the first record that meets the condition. it is a method in the advanced model class. for details, refer to getN ()
.

The following example uses the switchModel () method to locate a query:




  1. Public function read (){
  2. $ Dao = M ('user ');
  3. $ List = $ Dao-> switchModel ('adv ')-> where ('uid> 10')-> order ('uid ASC')-> first ();
  4. }

This method actually executes getN (0)
Method.
Last ()

The last () method is used to obtain the last record that meets the condition. it is a method in the advanced model class. for details, refer to getN ()
.

The following example uses the switchModel () method to locate a query:



  1. Public function read (){
  2. $ Dao = M ('user ');
  3. $ List = $ Dao-> switchModel ('adv ')-> where ('uid> 10')-> order ('uid ASC')-> last ();
  4. }

The getN (-1) method is actually executed.

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.