The TP frame---model model layer---Do the modeling object

Source: Internet
Author: User

The TP frame----Model Model layer---------------Do the modeling object

What is the model layer used to do????

It is mainly used to do the operation of database access.

It also shows that the TP framework comes with a way to access the database, using model models.

How to use model models ???

To access the database using the model layer, you need to configure it, because you must configure some parameters of the database before you can connect, so find config.php

Configuration file, opening

These are about the configuration of the database,

To paste these into the config.php.

Be modified.

config.php configuration file (where configuration is modified)

/ * Database Settings */' db_type ' = ' mysql ',//database type ' db_host ' = ' localhost ',// Server address ' db_name ' + ',//database name ' Db_user ' = ' root ',//username ' Db_ PWD ' = = ',//password ' db_port ' = ' 3306 ',//port (default port for database) ' Db_pref                             IX ' = = ',//database table prefix ' db_params ' = = Array (),//database connection parameter ' Db_debug ' = = TRUE,//Database debug mode can be logged after the SQL log ' Db_fields_cache ' = false,//enable field cache (when developing the  It hit false, after the development was finished to true) ' db_charset ' + ' UTF8 ',//database encoding by default with UTF8 ' Db_deploy_type ' + 0, Database Deployment method: 0   Centralized (single server), 1 distributed (Master-slave server) ' Db_rw_separate ' and false,//database read/write whether separate master-slave effective ' db_master_num ' = 1,//read/write separation of the number of the master servers ' db_slave_no ' + ',//specify from server ordinal

  

Once configured, you are ready to connect to the database.

Write in this main controller.

First, do a method

Method of manufacturing operation

function db ()// Build Operation method // This operation method to operate the database, DB () method name can be random

{

                   // How to use the model layer???

//TP the model in the frame, we want to do the models will be written in the Mode folder . Each table in the database, corresponding to the TP frame is the TP framework of the class, a table corresponding to a TP framework within a class, such as the info table corresponding to the TP frame is a model class of info. Table corresponding to the class, table of fields (that is, columns) corresponding to the member variable. If you use the Info table, the model used is actually infomodel such a class, the Infomodel class if the info model does not do a special operation (if not the info table for special Operations) do simple additions and deletions can not do the model file, Can be empty even if the model folder does not do any of the models files can still manipulate the database.

The model file (model file), such as adding validation in the table, needs to be done only when the tables have been specifically manipulated.

How to use the Model class???

To manipulate the Hao table, how to do Hao model object???

There are three ways of making objects:

                   // 1. Original Way ( the class file can be found based on the namespace to new out. The original method must be done to make the model file, even if the model file inside the content is empty also to do, to have this file to find this file New out. To use the first primitive way, a file will be created under the Model folder.

Build a model file to manipulate the info table

To manipulate the info table, the table name begins (if the table name in the database is uppercase and uppercase, lowercase is the lowercase)

Model.Class.php ( Controller)

This is a class file. This class requires a namespace.  namespace Home\model; This namespace starts from the root and its root is the Model folder under the Home,home module

This class also inherits the parent Model class, the parent class-----Think------Model.class.php

So use it as the parent class, using Think\model; Think represents the root of this class, and the Think folder has a class file called model. class.php

class Infomodel extends Model

/* $m = new \home\model\infomodel ();

Var_dump ($m); * *

The results displayed:

Objects of the Infomodel

Information about the database

Where to look, right?

If there is no problem with the info table, all fields of the info table are displayed here. Can see this page, the model object can be created, and the database configuration is not a problem, can find this table

// 2. Use the shortcut method D()

$m = D (); Returns an object

Var_dump ($m);

The results displayed:

Create a parent class object, you can not find the information of this table. I couldn't find the information for this table because I didn't specify a table. The object that created the parent class is not the information for the table. When you create an object, you do not specify which table to manipulate, and the object that is created is a parent object, and when the parent object is manipulated, such as to invoke Add, add data, and do not know which list to add, there will be a problem.

So when you build it, give it a table name.

$m = D ("Info"); /// returns an object

Var_dump ($m);

Show Results:

the object of the creation is Infomodel Object


It also shows that, without making model files, the Infomodel object can still be generated through the D () method

D () method, is the new version of the reduction

         // 3. Use the shortcut method M()

$m = m ();

Var_dump ($m);

Show Results:

created a parent class object

instructions, using M () method can also be used to create model objects

//$m = m ("Nation");

//var_dump ($m);

Show Results:

Object is a parent class object

this M () method and D () method is different. The D() method is equivalent to the simplified version of new, but the M() method is to create the parent class object.

M () method, write parameters and no write parameters are different.

This table is available for writing parameters. Show Results:

You can manipulate this table as long as the field information for the table appears. For example, field information:

The TP frame---model model layer---Do the modeling object

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.