Thinkphp entry 5-model (49), thinkphp49. Thinkphp entry 5-model (49), thinkphp49 [Database operation model] model database operation tp framework main design mode: MVCC: controller shopLib Thinkphp entry 5-model (49 ), thinkphp49
[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]: [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]: [demonstration of data obtained through the model]: [method of instantiating the model]
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 ]:
Fifth-model (49), thinkphp49 [Database operation model] model database operation tp framework main design mode: mvc c: controller shop/Lib /...