Try to use the MONGO model in thinkphp today and find out that there will always be an error
Call to a member function selectcollection () on a non-object
For a reason, it was found that the configuration file was not imported, resulting in the inability to connect to the database and instantiate the model. Look at the configuration file, and there are no errors, the configuration file is as follows
Return Array (
' Config item ' = ' config value '
' Db_config ' = Array (' db_type ' = ' MONGO ',
' db_host ' = ' localhost ',
' Db_name ' = ' mydb ',
' Db_user ' = ',
' Db_pwd ' = ',
' Db_port ' = ' 27017 ',
' Db_prefix ' = ',
' Db_charset ' = ' UTF8 '
)
);
In model:
protected $connection = ' db_config ';
So instead of using another way of writing, the model is defined directly and found to be successful.
Protected $connection = Array (' db_type ' = ' MONGO ',
' db_host ' = ' localhost ',
' Db_name ' = ' mydb ',
' Db_user ' = ',
' Db_pwd ' = ',
' Db_port ' = 27017,
' Db_prefix ' = ',
' Db_charset ' = ' UTF8 '
);
Continue trying to change the configuration file to:
Return Array (
' Config item ' = ' config value '
' Db_type ' = ' MONGO ',
' db_host ' = ' localhost ',
' Db_name ' = ' mydb ',
' Db_user ' = ',
' Db_pwd ' = ',
' Db_port ' = ' 27017 ',
' Db_prefix ' = ',
' Db_charset ' = ' UTF8 '
);
Delete the definition of db_config, return directly, find also can connect successfully.
It's not clear. For the MONGO model, TP does not support the first configuration method, but it does have this configuration method in the documentation, and it can be used correctly for MySQL.
thinkphp MONGO Model Database configuration