Model with joined tables |
Hi
How should I work with model data that is stored in several tables as intended by the framework? Should I override the save method in the main Model and write data in my code? Or should I create additional models for all related tables? With this approach, should I save / read nested models by overriding the save method or work with them separately from the controller?
For your problem I would use CodeIgniter 4 Standard Query.
CodeIgniter 4 User Guide - Database Quick Start: Example Code What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
But.. 90% of cases the data are distributed on several tables:
A user can have several avatars, which are stored in a separate table. The service can have several tariff plans. Article - multiple authors... The model does not have the ability to work with nested (related) models or tables? I can not use models for it? I view the Model as a Repository
Maybe u can add callbacks on model to update/insert related data on other tables like on afterInsert and afterUpdate event.
What are nested models?
Why not creating additional models? Then you can use the save method for every model. Unfortunately CI has no option for using relations in models.
I have a userModel
Code: [users] Every user have same avatar images Code: [avatars] When I got User entity from userModel I wanna access to PHP Code: $User->avatars($only_default = false); // return plain array or array of entities of Avatar() Yes, I can load avatars directly in method. But what if i need build a table of users with avatars? I would not like to make 50 requests in a cycle. How to do it right? Given that I still need a list of social networks and etc Maybe something that PHP Code: $Users = $userModel->withAvatars()->get([1,2,3]); Or only this way? PHP Code: $Users = $userModel->get(...); (sorry for my english) |
Welcome Guest, Not a member yet? Register Sign In |