thinkphp Framework Model (database query), thinkphp framework _php Tutorial

Source: Internet
Author: User

thinkphp Framework Model (database query), thinkphp framework


1. Model definition

The file name must be a table name +model.class.php for example: UserModel.class.php

namespace Home\model;     // the namespace of the model class  Use  Think\model;             // The following model classes need to inherit a model base class, which is a system-defined class of models, in the Think\model namespace class extends model{    }

The class name in the file is consistent with the "table name" in the filename ( this is necessary if the model class is not found in the controller).

The correspondence between the class name and the table name is ( Note: The data table for each uppercase corresponding to the model name has an underscore in front of it, the second line below )

Model name Contract the corresponding data table (if the database prefix is THINK_)
Usermodel Think_user
Usertypemodel Think_user_type

2. Model instantiation

2.1 Direct instantiation

2.1.1 Without parameter instantiation $user =new \home\model\usermodel ();

2.1.2 with parameter instantiation $user =new \home\model\usermodel ([' Model name '],[' data table prefix '],[' database connection information ']);

The difference between the two methods is that the first one goes back to read the default configuration when it is instantiated (for example, in a model configuration or a database configuration in a module), while the second method is to read directly the database configuration of the incoming parameter

The model name is the name of the table in the corresponding database that is stripped of the prefix, and if you do not specify this will read the name of the model class, but it is the names of the models removed, for example:

The Useremodel class in the UsereModel.class.php file, if passed in the model name "user" in the instantiation, will press the incoming Lookup data table prefix + "user", and if not, extract the "Usere" of the Useremodel class to find the data table. This solves the problem that the model name is inconsistent with the data.

2.2 D Method Instantiation

The parameter of the D method is the name of the model and is consistent with the capitalization definition of the model class.

 
  PHP   // instantiation model $user=d ("user"); //

Cross-module instantiation

D (' Admin/user ') where "Admin" is the module name, "User" is the model name

Instantiating the info model under the Extend extension space

D (' Extend://editor/info ')

2.3 m Method instantiation

The difference between the M (' User ') and the D method instantiation is that the method does not require the model to exist, and if the model exists, the method is not ignored, which is primarily used to manipulate the database.

The M method is called by default when the D method does not find the model.

2.4 Instantiating an empty model

$Model =new Model () or $Model =m ();

This empty instance can be used to make native SQL queries ( Note: Other models can also make native SQL queries, and the queried table does not have to correspond to the model )

3. Fields in the model

$fields =array (', ') represents a field in a datasheet

$PK represents the primary key for a data table

$connection =array (' db_type ' = ' mysql ', ' db_user ' + ' root ', ' db_pwd ' = ' 1234 ',' db_host ' = ' localhost ' ,' db_port ' = ' 3306 ',' db_name ' = ' thinkphp ',' db_charset ' and ' UTF8 ', ' Db_params ' = = Array (),//not required ); Represents a database connection

Database type://user name: password @ Database address: Database port/Database name # Character Set

$connection = ' Mysql://root:1234@localhost:3306/thinkphp#utf8 ';

http://www.bkjia.com/PHPjc/1117387.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117387.html techarticle thinkphp Framework Model (database query), thinkphp Framework 1. The model definition file name must be a table name +model.class.php for example: UserModel.class.php namespace Home\model; The mold ...

  • 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.