Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
rolepermissions sets old records role_id to 0
  • I am currently trying to make it easy to control my group, roles and permissions instead of having to write them directly in the database. It was easy to make page that could update what roles was assigned to each group. My problem starts when i want to assign the permission and action to each role.

    $role = \Model\Auth_Role::find($role_id);
    foreach(Input::post('permission') as $perm_id => $actions)
    {
    $perm = \Model\Auth_Permission::find($perm_id);
    $permission_array[] = \Model\Auth_Rolepermission::forge(
    array(
    'role_id' => $role->id,
    'perms_id' => $perm->id,
    'actions' => $actions,
    ));
    $role->rolepermission = $permission_array;
    $role->save();
    }

    The messy part is eveytime i update a roles permission, the old entries role_id is set to 0 instead of being deleted. So right now i have around 400 entries, where only a few of them is currently in use.

    Can someone tell me if what I might be doing wrong?
  • There is a many-to-many relation, so permissions are not deleted unless you do that yourself. Which is a good thing, the same permission record could be assigned to other roles, to groups or to users...
  • I think you misunderstood me. The problem is not the permission table, but the table called *_role_permissions that keeps the relations between roles and permissions.

    id	role_id		perms_id	actions
    1 1 1 a:3:{i:0;s:1:"0";i:1;s:1:"1";i:2;s:1:"2";}


    when permisison for role 1 is update the following is in my table

    id	role_id		perms_id	actions
    1 0 1 a:3:{i:0;s:1:"0";i:1;s:1:"1";i:2;s:1:"2";}
    2 1 1 a:1:{i:0;s:1:"1";}
  • Ah, ok. It depends.

    If you create a relation, you should start with a Rolepermission object, and assign it a Role and a Permission. And possibly a subset of actions.

    If you want to delete a relation, either fetch the Role or the Permission, and unset the related object. Or fetch the Rolepermission itself and delete it.

    This is because it is both a junction table (to create the many-many between role and permission) and a standard table (due to the action column being there).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion