sqlyiimany-to-manyyii-cmodel

MVC - Many-to-Many relations--their own models?


My straight forward question:

If I have a many-to-many relation in my database (as the following tables)

User          Role          UserRole
  user_id       role_id       user_id
                              role_id

Should UserRole have its own Model when connecting this database to a MVC framework?

In context:

I'm rewriting my completely self-written web service with a MVC framework and I decided to use Yii. I have the option to generate a lot of code from my database tables but I got confused whether to Model the many-to-many relation tables (like UserRole above).

Thanks in advance!


Solution

  • I mostly use the code-first approach and there defining the relation table is optional for many-to-many relationships. Going from this, I'm assuming that the MVC framework in general considers this to be optional. So, the answer would be:

    Must UserRole have its own Model? No.

    Can UserRole have its own Model? Yes.

    Should UserRole have its own Model? If your many-to-many only contains two id's, I would not add it to the model.