문제

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!

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top