Home >Backend Development >PHP Tutorial >Solving the naming problem of belongsToMany() module name in thinkphp5
The content of this article is about solving the naming problem of belongsToMany() module name in thinkphp5. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
I recently encountered a problem when using TP5 to develop a CRM backend management system. When using related queries (many-to-many), the belongsToMany function is used to prompt that the model does not exist. Let’s take a look at the specific problem.
My usage in the project
Let’s take a look at my model in TP5 Layer
f_role is an association model
ref_user_role is an intermediate table (a many-to-many relationship requires an intermediate table)
role_id foreign key name
user_id is the primary key of f_user
This writing method is written completely in accordance with the manual. In theory, there is no problem at all, but when running, an error is reported:
It prompts that the FUser module does not exist, but my module name is f_user. A naming method.
The reason is that there is a problem with the naming rules of the module name.
After a short debugging, we found the cause of the problem:
Need to modify the parseModel method in the belongsToMany method in thinkphp/think/model.php
Just change the 1 in the picture to 0. The reason is that in the picture below
Just change the naming style
Finally change the naming style and then run the test.
Obtain data (including associated data can be obtained successfully).
The above is the detailed content of Solving the naming problem of belongsToMany() module name in thinkphp5. For more information, please follow other related articles on the PHP Chinese website!