: 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 ]:
- Create model file shop/Lib/Model/GoodsModel. class. php
- Set Database name, user name, password, and table prefix information in config. php.
- 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]
- The traditional method is new GoodsModel ().
- 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.