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

不言
Release: 2023-04-03 18:14:01
Original
5241 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!