Thursday, June 6, 2013

Using Roles and RolesUser

Hello Everyone,


       Im sorry if this was posted previously I did an exhaustive search but im still lost.  I am working on a SaaS app that allows a couple of our domains to utilize the same codebase and I have come upon an issue with my  1 User to Many Roles.  Im using cakePHP 2.3

I use 2 tables   a Roles (roles) and a RolesUser (roles_users)  Now this is working fine IF i was using separate code for each  my problem is I need to modify and pass in a site_id so only roles_users associated to the site_id,user_id are pulled.

I am using this in my User model

public $hasAndBelongsToMany = array(
'Role' => array(
   'className' => 'Role',
   'joinTable' => 'roles_users',
   'foreignKey' => 'user_id',
   'assosciationForeignKey' => 'role_id',
   'unique' => 'keepExisting',
)
    );

I also have a beforeFind:

public function beforeFind(array $query) {
$query['joins'] = array(
   array(
'table' => 'roles_users',
'alias' => 'RolesUser',
'type' => 'INNER',
'conditions' =>
array('User.id=RolesUser.user_id')),
   array(
'table' => 'roles',
'alias' => 'Role',
'type' => 'INNER',
'conditions' =>
array('RolesUser.role_id=Role.id')));
           return $query;
}

Cause I am trying to use TinyAuthorize.php (Im flexible here if I need to roll my own override of BaseAuthorize)

When I login the SQL dump is 

SELECT 
       `Role`.`id`, `Role`.`alias`, 
       `RolesUser`.`id`,
       `RolesUser`.`site_id`,
       `RolesUser`.`user_id`, 
       `RolesUser`.`role_id`
 FROM 
     `demoleag_league`.`roles` AS `Role` 
JOIN 
     `demoleag_league`.`roles_users` AS `RolesUser` 
ON 
      (`RolesUser`.`user_id` = 3 AND `RolesUser`.`role_id` = `Role`.`id`)

I cant find where this query is being generated

Can anyone point me in a direction  

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: