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

tanzania411
tanzania411

Hello, I would like to make a plugin for a ballet school and have some troubles with the relation management. DB-structure looks like this: tables: courses id title slug description

teachers
    id
    first_name
    last_name
    email

rooms
    id
    name
    description

lessons
    id
    day
    time_from
    time_to
    teacher_id
    room_id

courses_teachers
    course_id
    teacher_id

relations:
    courses -> teachers     belongsToMany
    teachers -> courses     belongsToMany
    courses -> lessons  hasMany
    lessons -> courses  belongsTo
    lessons -> rooms    hasOne
    lessons -> teachers     hasOne
    rooms -> lessons    belongsTo
    teachers -> lessons     belongsTo

My questions: Are these relations correct and where I have to place which config_relation.yaml

Cpt.Meatball
Cpt.Meatball

I might be somewhat late here, but here are my thoughts:

Your fields are defined right, although your models should be named without an 's'. (not plural, but singular: teacher instead of teacherS, rooms instead of room etc..). Next to that you seem to be switching out the belongs/has relationships:

The teachers->lessons relationship. A teacher HasMany lessons (a lesson belongsTo a teacher).

HasOne/HasMany

A teacher hasMany lessons. This means that the lessons table has the foreign key: teacher_id . BelongsTo is just the opposite. If a model Foo belongsTo model Bar, it means that Foo has the Foreign Key: Bar_id.

See This link for some more explanation.

Oh and remember: Courses -> Teachers will need a pivot table because of the many->many relationship!

1-2 of 2

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