In the HomeController directory of thinkphp3.2, the new TestoneController inherits the Controller, with an index method. A new TestoneModel inherits the Model and sets a test method. Two classes, for example, access the index of the Testone module later...
Use thinkphp3.2
In the \ Home \ Controller directory, the new TestoneController inherits the Controller, and there is an index method. A new TestoneModel inherits the Model and sets a test method. Two classes:
Then access the index method of the Testone module. url:
Http: // localhost/weixin/index. php/Home/Testone/index
The following error is reported:
After TestModel does not inherit the Model
Access again
Http: // localhost/weixin/index. php/Home/Testone/index
Access successful
But according to the official manual, shouldn't it be inherited? I hope I can answer the question about namespaces and inheritance?
Reply content:
Use thinkphp3.2
In the \ Home \ Controller directory, the new TestoneController inherits the Controller, and there is an index method. A new TestoneModel inherits the Model and sets a test method. Two classes:
Then access the index method of the Testone module. url:
Http: // localhost/weixin/index. php/Home/Testone/index
The following error is reported:
After TestModel does not inherit the Model
Access again
Http: // localhost/weixin/index. php/Home/Testone/index
Access successful
But according to the official manual, shouldn't it be inherited? I hope I can answer the question about namespaces and inheritance?
I tested it according to your code and found it was okay. Is it because you used the D Method to instantiate the model and leave the cache?
Public function _ construct ($ name = '', $ tablePrefix ='', $ connection = '') {// model initialization $ this-> _ initialize (); // obtain the model name if (! Empty ($ name) {if (strpos ($ name ,'. ') {// supported database name. model name definition list ($ this-> dbName, $ this-> name) = explode ('. ', $ name) ;}else {$ this-> name = $ name ;}} elseif (empty ($ this-> name )) {$ this-> name = $ this-> getModelName ();} // set the table prefix if (is_null ($ tablePrefix )) {// If the prefix is Null, no prefix $ this-> tablePrefix = '';} elseif (''! = $ TablePrefix) {$ this-> tablePrefix = $ tablePrefix;} elseif (! Isset ($ this-> tablePrefix) {$ this-> tablePrefix = C ('db _ prefix ');} // database initialization operation // obtain database operation objects // The current model has independent database connection information $ this-> db (0, empty ($ this-> connection )? $ Connection: $ this-> connection, true);} the constructor of the model connects to the database. Generally, a model corresponds to a database table.
After testing your code, there is no problem:
test(); }}?>
Output result:
When the Model class is instantiated, it will connect to the database by default and find the table name that is the same as the class name. If you have not added the corresponding table before, an error will be reported.
If your Model is:TestOneModel
, The database should exist: xx_test_one table.
Http://www.kancloud.cn/manual/thinkphp/1728
This error occurs because the database is not configured.
Configuration in config. php
// Database configuration information 'db _ type' => 'mysql', // Database TYPE 'db _ host' => '2017. 0.0.1 ', // server address 'db _ name' => 'thinkphp', // database NAME 'db _ user' => 'root ', // username 'db _ pwd' => '123', // password 'db _ port' => 123456, // PORT 'db _ PARAMS '=> array (), // database connection parameter 'db _ prefix' => 'think _ ', // database table PREFIX 'db _ charset' => 'utf8 ', // Character Set 'db _ debug' => TRUE, // you can record SQL logs after enabling the database debugging mode.
And the database must havePrefix _ test_one
Table