This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

intosite
intosite

i have an 3 models, User, Gym & GymUser

//User
   -id

//Gym
   -id

//GymUser
   -id
   -user_id
   -gym_id

As such i set my plugin to extend the User Model

UserBase::extend(function($model) {
            $model->hasManyThrough['gyms'] = [
                    'myplaylab\Models\Gym',
                    'key'        => 'gym_id',
                    'through' => 'myplaylab\Models\GymUser',
                    'throughKey' => 'id'
            ];
}

The problem is it's using the value from the GymUser's id col to match against the Gym's id, when i need is to use the value from the user_id col.

intosite
intosite

Managed to solved it, I was using the wrong relationship type. Should be using belongsToMany

UserBase::extend(function($model) {
            $model->belongsToMany['gyms'] = [
                    'myplaylab\Models\Gym',
                    'table'    => 'myplaylab_gym_user',
                    'key'      => 'user_id',
                    'otherKey' => 'gym_id'
            ];
}

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.