1, model definition. In thinkphp, you can eliminate the need for any model definition. Model classes must be defined only when they need to encapsulate separate business logic, so thinkphp has a lot of flexibility and convenience on the model, so you don't have to worry about too many tables.
A model class typically inherits the \think\model class or its subclasses of the system, and the following is the definition of a Home\model\usermodel class:
namespace Home\model;use Think\model; class Categorymodel extends Model { protected'top_categories'; protected ' Top ' ;}
2, model instantiation. Depending on the model definition, we have several methods for instantiating the model, depending on the needs of the different ways:
(1) Direct instantiation. Model classes can be instantiated like other class libraries, and model classes are usually \think\model classes that inherit the system, for example:
New \home\model\usermodel (); // instantiated with parameters $New New \home\model\newmodel ('blog',' Think_', $connection);
(2) D method instantiation. The D method automatically detects the model class and, if there is a custom model class, instantiates the custom model class, and if it does not, instantiates the system's \think\model base class, and does not repeat the instantiation for the instantiated model.
$User = D ('User');
(3) m method instantiation. When the D method instantiates a model class, it is usually instantiated with a specific model class, and if you are simply curd the data table, using the M method instantiation, the performance will be higher because you do not need to load the specific model class.
$User = M ('User');
(4) Instantiate an empty model class.
// instantiate an empty model New Model (); // or using the M shortcut method is equivalent $Model = M (); // make a native SQL query $Model->query ('SELECT * from think_user WHERE status = 1');
3, database connection information. The system reads the relevant parameters in the configuration file by default, including:
'Db_type'="',//Database Type'Db_host'="',//Server Address'db_name'="',//Database name'Db_user'="',//User name'db_pwd'="',//Password'Db_port'="',//Port'Db_prefix'="',//database table Prefixes'Db_dsn'="',//database connection DSN for PDO mode'Db_charset'='UTF8',//the encoding of the database defaults to UTF8
Both the direct instantiation of the above instantiation method and the M method instantiation can be passed into the database connection information. The database connection information parameter supports three formats:
(1) String definition
new \home\model\newmodel ('blog','think_', ' Mysql://root:[email Protected]/demo ');
(2) Array definition
$connection =Array ('Db_type'='MySQL', 'Db_host'='127.0.0.1', 'Db_user'='Root', 'db_pwd'='12345', 'Db_port'=3306, 'db_name'='Demo', );New\home\model\newmodel ('New','Think_', $connection);
(3) Configuration definition
//Database Configuration 1'DB_CONFIG1'=Array ('Db_type'='MySQL', 'Db_user'='Root', 'db_pwd'='1234', 'Db_host'='localhost', 'Db_port'='3306', 'db_name'='thinkphp'),//Database Configuration 2'Db_config2'='Mysql://root:[email protected]:3306/thinkphp',
Then we can instantiate the model class incoming connection information like this:
new \home\model\newmodel ('new','think_',' db_config1'); new \home\model\blogmodel ('blog','think_ ','db_config2');
thinkphp Development Notes