CodeIgniter Forums
Problem with associations - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem with associations (/showthread.php?tid=52949)



Problem with associations - El Forum - 07-03-2012

[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)


Problem with associations - El Forum - 07-04-2012

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


Problem with associations - El Forum - 07-04-2012

[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



Problem with associations - El Forum - 07-04-2012

[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...