help with relationships

Hi yii members.

I’m currently working with a development poject in which i need to interact with objects (from the domain model) and the database tables. In this case, the relationship between these particular tables is n --> n (many to many) but the interaction is done between 3 tables.

I’m using the guide stored on the http://www.yiiframew…en/database.arr URL and, on it, i can see an example of how to work with 2 tables. I have some doubts about the way the relationships work on active records when you have more than 2 tables (in this case, 3).

This is the schema I have in the database I’m working on.

tbl_module (

module_id int(11) NOT NULL AUTO_INCREMENT,

module_name varchar(100) NOT NULL,

PRIMARY KEY (module_id),

UNIQUE KEY module_name (module_name)


tbl_rel_action_module (

acmo_action_id int(11) NOT NULL,

acmo_module_id int(11) NOT NULL,

PRIMARY KEY (acmo_action_id,acmo_module_id)


tbl_action (

action_id int(11) NOT NULL AUTO_INCREMENT,

action_name varchar(100) NOT NULL,

PRIMARY KEY (action_id)


tbl_rel_action_module_role (

acmoro_role_id int(11) NOT NULL,

acmoro_action_id int(11) NOT NULL,

acmoro_module_id int(11) NOT NULL


tbl_role (

role_id int(11) NOT NULL AUTO_INCREMENT,

role_name varchar(100) NOT NULL,

role_created datetime NOT NULL,

role_created_user_id bigint(20) NOT NULL,


role_updated_user_id bigint(20) DEFAULT NULL,

role_status_id int(11) NOT NULL,

PRIMARY KEY (role_id)


role >-< module>-<action

I was wondering if someone could give me a hand with the "active record relationship" when working with the table "tbl_rel_action_module_role" as I need to get all the objects data at one time (maybe using with or any other relationship query options.

This is an example of the data I’m receiving in any case.



columns ->id|name

data-> 1 |admin


columns ->id|name

data-> 1 |Address

Relationship between action and module


columns → acmo_action_id|acmo_module_id

data-> 1 | 1

2 | 1


columns → id|name

data-> 1 | view

2 | create

3 | update

Relationship between action, module and role


columns → acmoro_role_id | acmoro_module_id |acmoro_action_id

data-> 1 1 1

1 1 2

This is the data i should be getting on my code (and what is causing this doubts)


-role_id (attribute) ->1













I really appreciate your help on this.

Thanks in advance

//in model table role 	

public function relations(){

return array(

'rolaction' =>array(self::HAS_MANY,'rolactionMODEL','acmoro_role_id','on'=>'role_id=acmoro_role_id');

'modules' =>array(self::HAS_MANY,'modulesMODEL','module_id','on'=>'acmoro_module_id=module_id');

'modulesaction' =>array(self::HAS_MANY,'modulesactionMODEL','acmo_module_id','on'=>'module_id=acmo_module_id');


'modulesactiontableaction' =>array(self::HAS_MANY,'actionMODEL','action_id','on'=>'acmo_action_id=action_id');



//in model class  tbl_rel_action_module_role 	

public function relations(){

return array(

'modules' =>array(self::BELONGS_TO,'rolactionMODEL','module_id','on'=>'module_id=acmoro_module_id');


//in model class tbl_rel_action_module

public function relations(){

return array(

'modulesaction' =>array(self::BELONGS_TO,'modulesMODEL','acmo_module_id','on'=>'acmo_module_id=acmoro_module_id');


//in model class tbl_action

public function relations(){

return array(

'modulesactiontableaction' =>array(self::BELONGS_TO,'actionMODEL','action_id','on'=>'action_id=acmo_action_id');



$dataProvider=new CActiveDataProvider('rolMODEL',array( 'criteria'=>array(