0

I'm trying to solve an application challenge in CakePHP 3.x

I have employees and I have seminars. Employees can have several seminars and seminars can have several employees. The catch though is that the role of each employee at each seminar can be different. An employee's relationship to a seminar could be Attendee, Host, Instructor, or Manager.

I know how to a do a classic many-to-many using a third table like such as

class SeminarsTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('Employees', ['joinTable' => 'seminar_employee',]);
    }
}

I'm sure I can cobble together any number of solutions to get it to function, but I'd like to stick to Cake conventions. Such as the join table being named "seminar_employee"

But how do I have four total relationships that facilitate different roles while sticking to convention?

2
  • You could add a field "role" in your seminar_employee table which mention the role for each employee at each seminar. "I'd like to stick to Cake conventions" if you want to stick to Cake conventions your table has to be named employees_seminars. In Cake 3.X the joining table is named after the two tables involved, in alphabetic order, separated with an underscore by convention. With this name you can do your join like that: $this->belongsToMany('Employees'); Jul 28, 2015 at 9:41
  • Oh okay so I need to swap the order of the tables and pluralize them ("employees_seminars") and then have three columns in that table (employee_id, seminar_id, role) How would I select this role field?
    – eagers
    Jul 29, 2015 at 4:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy