Thinkphp creating objects and manipulating data

Source: Internet
Author: User

Thinkphp has three ways to create controller objects:

    1. Find the class through the path to the namespace and then new out for example: $DX = new \home\controller\indexcontroller ();
    2. Create Object A ("module/Controller") with a shortcut function for example: $DX = A ("Home\index");
    3. Create an object by using the R shortcut function and invoke the method; R ("Index/shuchu")

thinkphp Operation Database:

First, you need to configure the database information in the configuration file

Executing SQL statements in creating model objects

There are three ways of creating model objects:

    1. The original mode requires a model model class, the class name must be a database table name, for example: $m = new \home\model\infomodel ();
    2. Use the shortcut function D: $m = d ("Info"); It is important to note that if no model class can have no arguments, the object is the object of the models parent class

3, using the shortcut function m: $m = m ("Nation");

Operational database:

Let's start with a coherent function, that is, the return value is: $this, that is, return the object itself, you can continue to call the function

Basic functions in Database operations: Select ("PRIMARY key value", primary key Value "" "), find (" PRIMARY key Value "), aggregate function is not a coherent function.

The specific usage is as follows: (Refer to Manual)

manipulating databases
$attr = $m->select (); Querying all data
$attr = $m->select ("p001,p002,p003");
$attr = $m->find ("P001"); Find specific data based on the primary key value

Where can add query criteria
$attr = $m->where ("code= ' p001 ' or sex=true")->select ();

Table to toggle tables to manipulate
$attr = $m->table ("Nation")->select ();

Alias can set aliases for a table
$attr = $m->alias ("Personnel")->select ();

field can specify fields for the query
$attr = $m->field ("Code,name")->select ();

Order can be sorted by criteria
$attr = $m->order ("Nation desc")->select ();

Group can be grouped
$attr = $m->field ("Nation")->group ("Nation")->select ();

Conditions after having the ability to add groups
$attr = $m->field ("Nation")->group ("Nation")->having ("Count (*) >5")->select ();

Join can join multiple tables and Alias fields in field
$attr = $m->field ("Info.code as code, info.name as name, sex as gender, nation.name as National name")->join ("Nation on info.nation = Na tion. Code ")->select ();

Union Union query
$attr = $m->field ("name")->union ("Select name from Nation")->select ();

Distinct to Heavy
$attr = $m->field ("Nation")->distinct (True)->select ();

Limit can be paged, the first parameter represents how many to skip, the second represents how many bars
$attr = $m->limit (10,5)->select ();

Page can be paged. The first parameter represents the current page, and the second parameter represents how many bars per page
$attr = $m->page (3,5)->select ();

Fetch data total number of bars
$attr = $m->count ("*");
Take a column of and
$attr = $m->table ("Car")->sum ("Price");
Take average
$attr = $m->table ("Car")->avg ("Price");
Take maximum value
$attr = $m->table ("Car")->max ("Price");
Take the minimum value
$attr = $m->table ("Car")->min ("Price");


$sql = "SELECT * from Info where nation= ' n001 '";
$attr = $m->query ($sql);

$sql = "INSERT into Nation values (' n104 ', ' by actual ')";
$attr = $m->execute ($sql);

$attr = $m->field ("Info.code as code,info.name as name,sex,nation.name as Nationname,birthday")->join ("Nation on Info.nation = Nation.code ")->select ();

$this->assign ("info", $attr);

$this->assign ("Test", 10);

$this->display ();

    

Thinkphp built-in label

<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0";
<tr>
<td> Code </td>
<td> name </td>
<td> gender </td>
<td> national </td>
<td> Birthday </td>
</tr>

<tr>
<td><{$v. code}></td>
<td> <{$v. name}></td>
<td> ></td>//Three mesh operator does not support point operation
<td><{$v. nationname}></td>
<td ><{$v. birthday}></td>
</tr>
</foreach>


</table>

<if condition= "$test gt" > //condition is the condition, as is the label in order to prevent ambiguity with the alternate word
Hello 5
<else/>
Hello 10
</if>

Thinkphp creating objects and manipulating data

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.