Active Record relations issue

Hi,

I am new to Yii. I have four models with the following relation.(please see the ER Diargam)

1420

erDiagram.jpeg

relations() in model ‘Role’ is:




		return array(

			'user'=>array(self::HAS_MANY, 'User', 'id_role'),

			'rolePermissions'=>array(self::HAS_MANY, 'RolePermissions', 'id_role','with'=>'permissions'),

		);



relations() in model ‘RolePermissions’:




		return array(

			'permissions' => array(self::BELONGS_TO, 'Permissions', 'id_permissions'),

			'role' => array(self::BELONGS_TO, 'Role', 'id_role'),

		);



relations() in model ‘Permissions’:




		return array(

			'rolePermissions'=>array(self::HAS_MANY, 'RolePermissions', 'id_permissions'),

		);



relations() in model ‘User’:




		return array(

			'organisation' => array(self::BELONGS_TO, 'Organisation', 'id_organisation'),

			'role' => array(self::BELONGS_TO, 'Role', 'id_role',

				'with'=>'rolePermissions'),

		);



I have successfully printed the role of each user from $data array(‘value’=>’$data[\‘role\’][\‘attributes\’][\‘name\’]’,) But I am unable to print the value of permissions field from permissions table.

print_r($dataProvider->data[2][‘role’][‘rolePermissions’]) This print statement will show the permission field value.

When I tried to print the value of permission in the following way, it returned an error ‘Undefined index: permissions’

‘value’=>’$data[\‘role\’][\‘rolePermissions\’][\‘permissions\’][\‘attributes\’][\‘permission\’]’,

Please help me to solve this issue.

Maybe this would help:


//let's assume you want the permissions of the active user


$user = Yii::app()->user->id;

$permissions = $user->role->rolepermissions->permissions;



This should give you an array of permission AR objects associated with this user. To read the field "permission" in the permissions table you could now type:


//this could be checked using foreach()

$permissionfieldarray = $permissions->permission;


foreach($permissionfieldarray as $permission)

{

if ($permission === 'delete')

{

echo 'I am able to delete!';

}

  

}

Starting from 1.1.7 you can actually relate to permissions using ‘through’ http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through




// from role

return array(

                        'user'=>array(self::HAS_MANY, 'User', 'id_role'),

                        'rolePermissions'=>array(self::HAS_MANY, 'RolePermissions', 'id_role'),

                        'permissions'=>array(self::HAS_ONE,'Permissions','id_permissions','through'=>'rolePermissions),

                );



Have you tried that?

Thank you for your help :)

First of all, thank you for your time and reply.

My Yii version is: 1.1.6

In this case, user has one role and role has multiple permissions.

I have updated the model relations as follows:




//Model 'User'

	return array(

		'organisation' => array(self::BELONGS_TO, 'Organisation', 'id_organisation'),

		'role' => array(self::BELONGS_TO, 'Role', 'id_role',),

	);

//Model 'Role':

	return array(

		'user'=>array(self::HAS_MANY, 'User', 'id_role'),

		'permissions'=>array(self::MANY_MANY,'Permissions','role_permissions			

		(id_role,id_permissions)'),

		);

//Model 'RolePermissions':

	return array(

		'permissions' => array(self::BELONGS_TO, 'Permissions', 'id_permissions'),

		'role' => array(self::BELONGS_TO, 'Role', 'id_role'),

	);


//Model 'Permissions':

	return array(

		'rolePermissions'=>array(self::HAS_MANY, 'RolePermissions', 'id_permissions'),

	);



All models extends CActiveRecord class.

$dataProvider->data[0][‘role’][‘permissions’][0][‘permission’]; This statement will print the value from the permission table outside the grid view. My grid is $this->widget(‘zii.widgets.grid.CGridView’, array(…). How can I print the value from the permission table inside the grid? Please check the above relations and tell me, if that seems to be incorrect.

thank you.

The way it is created, roles has many permissions, why do you wish to display ONE permission? How that is set? Shouldn’ t the permission be checked against a specific action to be able to be displayed on a grid? I don’t really understand how you wish to display just one permission when a role has many permissions. The difference is if you wish to display the information of a specific action that requires a specific permission for a specific role.