About thinkphp framework model notes

This is a model, the feeling of learning is not very clear, a separate study notes in a water model thinkphp.

 

0x01 model class Introduction

Database each table corresponds to a model class name is the table name, class member variables inside the column names,

The one table corresponds to a class in which a corresponding one of the data objects

If we do not have a special operation on the model table, then we can not build the model, but the C layer and the layer must have V

Class model simple code:

<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model
{
    public $tablePrefix ='';
    public $tableName='user';
    public $trueTableName='user';
    public $dbName='snatch';
     
    public function text() {
        print_r($this->db->query('select * from segment limit 1'));
        return "这是模型";
    }
}
?>

Class call control model instance:

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
echo "<meta charset='utf-8'>";
echo "hello world.";
$user=D('user');
$arr=$user->where(array('uid'=>'2','password'=>'fcea920f7412b5da7be0cf42b8c93759'))->order(array('uid'=>'desc'))->limit(1)->group('uid')->select();
echo $user->getLastSql();
print_r($user->query('select * from segment limit 1'));
trace('',$user->getLastSql(),'user');
trace('hahah',$user->text(),'user');
$u = D();
print_r($arr);
echo "<h1>_____________________________</h1>";
print_r($u->query("select * from case_text limit 0"));
$user->select(array('where'=>'uid>1','order'=>'password asc'));

}

Models have to mention the D and M functions.

 

D function and M function is built-in method Think PHP fast initialization of the model.

Difference between D and M function is a function, the function M can be initialized without a model class definition model, i.e. M function is more efficient when the native SQL execution .

$user= D('user');

This code representative of the class initialization Model UserModel below.

 

 

D function is instantiated Lib / Model below your current project module

If the module does not exist, then returned directly instantiate an object Model (the significance of and M) the same (function)

And M returns the object's instance of the Model Parameter .. $ name as its database table name to the database processing operation

 

The following three methods exemplified class instantiation model

 

0x02 model class instantiation

Three methods of new instantiated directly, and significance of the difference between M and D functions, the next focus of the difference between M and D functions.

new example

A new way: the need to create your own model file, which table model needs, Model which table to go to build: In Home / Model / in New xxxModel.class.php

<?php
namespace Home\Model;
use Think\Model;
class InfoModel extends Model
{
        
}

The controller corresponding to class files:

function ShowAll () 
{ 
    $ info = new new \ Home \ the Model \ InfoModel ();   // must be an absolute path starting from an initial namespace 
    var_dump ( $ info ); 
}

 

D function

Do not build the model file, instantiate an object of the parent class Model

info $ = D ( "Info");   // create a parent Think \ Model objects 
 var_dump ( $ info );

 

M function

Examples of parent Model

$info = M("Info");

Can directly call the parent class attribute inside Model, database-related operation is obtained, but no specific data table

This allows the operating table data Info

 

D function and M function supports native SQL query:

public function index(){
        echo "<meta charset='utf-8'>";
        echo "hello world.";
        $user=D('user');
        $arr=$user->query('select count(*) from word_a');
        trace('SQL',$user->getLastSql(),'user');
        print_r($arr);
    }
//生成SQL:
select count(*) from word_a:SQL
 
public function index(){
        echo "<meta charset='utf-8'>";
        echo "hello world.";
        $user=M();
        $arr=$user->query('select count(*) from word_a');
        trace('SQL',$user->getLastSql(),'user');
        print_r($arr);
    }
//生成SQL:
select count(*) from word_a:SQL

 

Queries pieces of data:

// Get a plurality of data according to the master key 
$ List = All the User :: ( 'l, 2,3' );
 // or using an array 
$ List = All the User :: ([l, 2,3 ]);
 the foreach ( $ List  AS  $ Key => $ User ) {
     echo  $ User -> name; 
} 
// use the array query 
$ List = All the User :: ([ 'Status' =>. 1 ]);
 // use closures query 
$ List = :: All the User ( function ( $ Query ) {
     $ Query -> WHERE ( 'Status',. 1) -> limit (. 3) -> Order ( 'ID', 'ASC' ); 
}); 
the foreach ( $ List as $key=>$user){
    echo $user->name;
}

 

Data table definition


ThinkPHP model in which there are several definitions of attribute data table name:

 

 

 

Model configuration options

 

Field Definitions
 

DB_FIELDS_CACHE can set parameters to automatically turn off field caching, if you frequently change the structure of the database at the time of development, and not want to cache data table field, you can add the following configuration in the project configuration file:

// Close the Field cache
'DB_FIELDS_CACHE' => false
Note: The following Debug mode due consideration to the data structure may change frequently, so the default is off field caching.

 

Global definitions
 

Common configuration is to add the module in the application configuration file or configuration file following configuration parameters:

// database configuration information

'Db_type' => 'MySQL', // database type 
'DB_HOST' => '127.0.0.1', // server address 
'the DB_NAME' => 'thinkphp', // database name 
'DB_USER' => 'root' , // username 
'DB_PWD' => '123456', // password 
'DB_PORT' => 3306, // port 
'DB_PARAMS' => Array (), // database connection parameters 
'DB_PREFIX' => 'think_', / / database table prefix 
'db_charset' => 'UTF8', // characters 
'DB_DEBUG' => TRUE ,// can log record SQL database after the debug mode is enabled

 

Guess you like

Origin www.cnblogs.com/-qing-/p/11291404.html