3

I have 3 tables users , roles and roles_users. The roles_users table have user_id and role_id

Now my models are Model_User, Model_Role, Model_User_Role.

How can i link the three models so that every user is associated to one role and one role has many users.

I am using ORM and i want to display the list of users with the name of there role.

Note: The table i have taken from Auth Module of Kohana.

jimy
  • 4,848
  • 3
  • 35
  • 52

2 Answers2

2

You can use $_belongs_to, $_has_one and $_has_many arrays to set relationship between the models. Kohana 3 :: ORM Relationships

sra
  • 23,820
  • 7
  • 55
  • 89
Leonid
  • 92
  • 10
  • If you want to make such relationship as you discribe why you don't exclude Model_User_Role table? You can add field "role_id" in users db table and set `protected $_belongs_to = arry( 'role' => array('model' => 'role', 'foreign_key' => 'role_id'), );` in Model_Users and `protected $_has_many = arry( 'user' => array('model' => 'user', 'foreign_key' => 'role_id'), );` in Model_Role – Leonid Jul 15 '11 at 09:21
  • but i am following the auth module of Kohana and it has that table – jimy Jul 15 '11 at 14:16
  • You can make your own auth models, based on /modules/auth/classes/model/auth/user.php and /modules/auth/classes/model/auth/role.php, and set required relations – Leonid Jul 18 '11 at 05:19
1

I did a many to many relation with roles and users. And will write business logic that a users will not have more than one roles.

jimy
  • 4,848
  • 3
  • 35
  • 52