Welcome Guest, Not a member yet? Register   Sign In
How many models for many to many relations
#1

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

(This post was last modified: 02-21-2022, 11:21 AM by MGatner.)

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'

User::getPages(): Page[]
    return model(PageModel::class)->findForUser($this->id);

PageModel::findForUser(int $userId): Page[]
    return $this->select('pages.*')
        ->join('pages_users''pages.id = pages_users.id')
        ->where('user_id'$userId)
        ->findAll(); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB