"Database Operations Model models"
Model Database Operations
TP Framework Main design pattern: MVC
C:controller Controller shop/lib/action/Specific controller
V:view view shop/tpl/grouping/template files
M:model Data Model shop/lib/model/Concrete model
"Create Model"
Principle of Creation: a data table corresponding to a data model
To create a model:
When the following information appears, our database does not have a user name and password configured config.php
"Database Use steps":
- Create model file shop/lib/model/goodsmodel.class.php
- Set the database name, user name, password, and table pre-information in the config.php
- Goodsmodel model objects can be instantiated directly inside the controller
"Database Configuration config.php": "Get related information from database-query"
We use the Select () method to obtain data information from the database, which returns a two-dimensional array,
If we want to get a record, we use the Find () method, which returns a one-dimensional array
$goods Select ()//Get all records, all field results
$goods, select (15)//Get record results for primary key equals 15
$goods Select ("10,15,20")//Get the primary key record information in the range 10, 15, 20
$goods, find (16); Give us a one-dimensional array to return the record information with the primary key equal to 16
$goods-getbyxxx (); According to the specific XXX field to do the Where condition query, one-dimensional array results returned. This method will take the __call () automatic condition method inside the Model.class.php.
"Capitalization problem": "Get data concrete presentation through model models": "Instantiating model Mode"
- The traditional way new Goodsmodel () can
- You can instantiate a model object with the shortcut function D ()
For example: D (' Goods '); Instantiating goods Model objects
D (); Instantiates a base class object that needs to specify the data table name when it is used
and primary key names, more complex, not recommended for direct use
3 instantiating a base class object by using the shortcut function m ()
For example: M () instantiates the Model.class.php corresponding object, which is not recommended for use
"To create a model, it is possible that the corresponding table prefix of this model is inconsistent with the other table prefixes, then the model needs to define its own real table name":?
Thinkphp Getting Started five-model (49)