Thinkphp entry 5-model (49)

Source: Internet
Author: User
: This article mainly introduces Thinkphp entry 5-model (49). If you are interested in the PHP Tutorial, refer to it. [Database operation model]

Model database operations

Main design pattern of tp framework: MVC

C: controller shop/Lib/Action/specific controller

V: view shop/Tpl/group/template file

M: model data Model shop/Lib/model/specific Model

[Create model]

Creation principle: a data table corresponds to a data model.

Create model:

When the following information appears, it indicates that our database has not configured the username and password config. php.

[Database usage steps ]:

  1. Create model file shop/Lib/Model/GoodsModel. class. php
  2. Set Database name, user name, password, and table prefix information in config. php.
  3. The GoodsModel object can be directly instantiated in the controller.

[Database configuration config. php ]:

Obtain related information from the database-query]

We use the select () method to obtain data information from the database. this method returns a two-dimensional array,

If we want to obtain a record, we use the find () method, which returns a one-dimensional array.

$ Goods-> select () // Obtain all records and field results

$ Goods-> select (15) // Obtain the record result with the primary key equal to 15

$ Goods-> select ("10, 15, 20") // Obtain the record information of the primary key in the range of 10, 15, and 20

$ Goods-> find (16); // returns record information with a primary key equal to 16 in the form of a one-dimensional array.

$ Goods-> getByXXX (); // query the where condition based on the specific XXX field. one-dimensional array results are returned. This method follows the _ call () automatic condition method in Model. class. php.

[Case sensitivity problem ]:

[Demo of getting data using the model ]:

[Instantiation model method]

  1. The traditional method is new GoodsModel ().
  2. You can instantiate model objects through the same shortcut function D ().

Example: D ('Goods '); instantiate a Goods model object

D (); instantiate the base class object. you must specify the "data table name" when using this object"

And the primary key name, which is complex and is not recommended for direct use.

3. instantiate a base class object through the shortcut function M ()

For example, M () instantiates the object corresponding to Model. class. php. this method is not recommended.

[When creating a model, it is possible that the table prefix corresponding to this model is inconsistent with that of other tables. Therefore, this model needs to define its own real table name ]:

?

The above introduces Thinkphp getting started 5-model (49), including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.