Question

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!

Was it helpful?

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.

OTHER TIPS

for a many-many relationship, your relation definition would be like: return array( 'roles'=>array(self::MANY_MANY, 'Role', 'user_role(user_id, role_id)'), ); with user_role being the join table and user_id and role_id being the foreign keys.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top