简体   繁体   中英

General CakePHP: hasMany to hasAndBelongsToMany

I've been handed a web site, written in CakePHP to modify, and I am loving it! The theme and documentation are wonderful; and I love how organized the code is.

My task is to modify a database relationship, between two tables, from being one-to-many to a many-to-many; or in cake terms from a “hasMany” to a “hasAndBelongsToMany”.

I created the new intermediary table (in the MySql database) for the many-to-many relationship, but I'm trying to figure out where to go from there.

Should I run bake now that the table is created? Would that be the best next step?

What's the best method to find out each of the files I'll need to change the code in? I've used Notepad++ and have done a Search->Find In Files… to look for the name of one of the tables in the relationship, within the code. But with the data being organized into MVC, I think there must be a better way to identify all the areas where the code I need to modify is. I know each table has a view, controller and a model. Are those three places all inclusive; or are there other files and folders I should look into too?

(I'm somewhat new to the MVC design pattern in PHP. I understand the concept; but I'm used to having PHP and HTML all in one file, and being able to see a visual in DW. So having the code organized in MVC with no visual, has been quite the learning curve for me. )

Thanks so much.

Running bake is probably not a good idea for an already running application.

This won't be a simple task unless you have a very straight forward application at least regarding those tables. First you need to identify the functional changes even before touching code. To do that you need a good understanding of the already underlying logic.

You should start with the two models involved. Are there custom finds and logic that will need to change? Then go to the controllers and ask the same. Decide if you'll need a model for the intermediate model. Try to understand the impact of the changes for the rest of the app. If you correctly identify these then it'll be easy to adapt them to the change.

Unfortunately your milage may vary depending on the complexity of the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM