• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with associations

#1
[eluser]Carmichael[/eluser]
I am using the php-activerecord ORM.

I am working on a permissions system. The system has three tables built in this way:
Code:
groups
id|name
1 |admin

permissions
id|permission|category
1 |  create  |  user
2 |   edit   |  user

permissions_map
group_id|permission_id
   1    |     1
   1    |     2

I want to know how I should build the associations in the Permission class.

This is what I want it to do:
Get group permissions by group id (from permissions_map)
Get group information (name) by group id (from groups)
Get permissions by group id (from permissions)

#2
[eluser]Carmichael[/eluser]
Bump. I really need help.

#3
[eluser]Carmichael[/eluser]
The sql looks like this
Code:
SELECT pm.group_id, pm.permission_id,
       p.id, p.permission, p.category,
       g.name
FROM permissions_map AS pm
INNER JOIN permissions AS p ON p.id = pm.group_id
INNER JOIN groups AS g ON g.id = pm.group_id
WHERE g.id = $groupId

#4
[eluser]Carmichael[/eluser]
Code:
class Permission extends ActiveRecord\Model
{
    static $has_many = array(
        array('groups'),
        array('groups', 'through' => 'permissions_map', 'foreign_key' => 'group_id'),
    );

}

class Group extends ActiveRecord\Model
{
    
    static $has_many = array(
         array('permissions', 'class_name' => 'Permission', 'foreign_key' => 'group_id')
    );
    
    public function get_group_permissions($groupId)
    {
        $permissions = Group::find($groupId);
        return $permissions;
        
    }
    
}

view
Code:
<?php foreach($permissions as $permission): ?>

    <?php echo $permission->name; ?>

<?php endforeach; ?>

Doesn't work...this is hard as fuck and the ORM documentation sucks...



Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.