![]() |
How many models for many to many relations - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31) +--- Thread: How many models for many to many relations (/showthread.php?tid=81356) |
How many models for many to many relations - rambco - 02-19-2022 Having three tables: - pages - users - users per page how many models would you create to manage them? I typically only create two models, one for pages and one for users. Would you also create a third model for the relationship table? RE: How many models for many to many relations - MGatner - 02-21-2022 I prefer to use one, and then have my Entity use the related model to get its relations. This will be rough bc I am mobile but something like: PHP Code: UserModel:: $returnType = 'App\Entities\User' RE: How many models for many to many relations - evansharp - 10-22-2024 (02-21-2022, 11:18 AM)MGatner Wrote: I prefer to use one, and then have my Entity use the related model to get its relations. This will be rough bc I am mobile but something like:Found this old thread with Google... Tatter\Relations seems heavy handed for my use case, in which there is only one Entity; which has itself several different relations. I like the approach above, but don't totally understand how to transfer the concept to my application. @MGatner does your pseudocode suggest that we extend all the relation-models involved with methods to support the Entity-level methods which populate the instance attributes? Or keep it all in the Entity's own model and do several DB connections from there to the related tables? My sense is that with the approach you wrote above, we'd keep the Entity population spread over several models, but I wan to know what you'd suggest 2 years later. RE: How many models for many to many relations - ANAS2025 - 04-13-2025 Yes, I would recommend creating a third model for the users_per_page table, especially if that relationship has additional data (like roles, timestamps, permissions, etc.). However, if the relationship is purely many-to-many without any extra fields, and you're using an ORM (like Eloquent in Laravel), then managing it with just two models (User and Page) is also fine, using a pivot table without a dedicated model. In short: ✅ 3 models – Best if the relationship has extra fields or you need to manage it directly. ✅ 2 models – Okay if the relationship is simple and handled through the ORM’s many-to-many relation features. Let me know what tech stack or framework you're using, and I can tailor the advice! |