Home  >  Article  >  Backend Development  >  Solving the naming problem of belongsToMany() module name in thinkphp5

Solving the naming problem of belongsToMany() module name in thinkphp5

不言
不言Original
2018-08-10 17:26:045275browse

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.

Function introduction

Solving the naming problem of belongsToMany() module name in thinkphp5

My usage in the project
Solving the naming problem of belongsToMany() module name in thinkphp5
Let’s take a look at my model in TP5 Layer
Solving the naming problem of belongsToMany() module name in thinkphp5
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:
Solving the naming problem of belongsToMany() module name in thinkphp5
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.

Solution

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
Solving the naming problem of belongsToMany() module name in thinkphp5
Solving the naming problem of belongsToMany() module name in thinkphp5
Solving the naming problem of belongsToMany() module name in thinkphp5

Just change the 1 in the picture to 0. The reason is that in the picture below
Solving the naming problem of belongsToMany() module name in thinkphp5
Just change the naming style

Finally change the naming style and then run the test.
Solving the naming problem of belongsToMany() module name in thinkphp5
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn