Welcome Guest, Not a member yet? Register   Sign In
Model with joined tables
#1

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?
Reply
#2

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 )
Reply
#3

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
Reply
#4

Maybe u can add callbacks on model to update/insert related data on other tables like on afterInsert and afterUpdate event.
Reply
#5

Maybe. What a best practice for this cases?
Reply
#6

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.
Reply
#7

I have a userModel

Code:
[users]
user_id | name | ...


Every user have same avatar images

Code:
[avatars]
avatar_id | user_is | uri | is_dafault | ...


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]);
$Users $userModel->withAvatars()->withSocials()->get([1,2,3]); 

Or only this way?

PHP Code:
$Users $userModel->get(...);
$Avatars $avatarModel->get(...);
foreach(
$Avatars AS $Avatar){
   $Users{$Avatar->user_id][] = $Avatar;




(sorry for my english)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB