Monday, December 10, 2012

Re: Trouble with model associations and query.

I seem to have resolved this, but I don't think it's the most efficient query:

public function getStudents(){

$userId = $this->Auth->user('id');

$endusers = $this->RoleUser->findAllByuser_id($userId, array('RoleUser.enduserid','RoleUser.user_id')); //Get the enduserid's for all the roles I have been assigned

foreach ($endusers as $enduser){

$theusers[] = $this->User->findById($enduser['RoleUser']['enduserid']); //store each user to an array.

    }

debug($theusers);

Any ideas how I can get the same result by just querying the Users table and associated roles/userroles ? }


On Sunday, December 9, 2012 2:30:38 PM UTC-5, MDay wrote:
I am having trouble returning a query.  I have the following setup

Users - users class of all users in the system.
Roles - roles class with a set of available roles.  E.g. teacher, student, tutor
RoleUsers - a mapping table between users and roles as a single user can have multiple roles.


My Users class has this

public $hasAndBelongsToMany = array(

        'Role' =>

            array(

                'className'              => 'Role',

                'joinTable'              => 'role_users',  //was roles_users

                'foreignKey'             => 'user_id',

                'associationForeignKey'  => 'role_id',

                'unique'                 => true)

);


My RolesUser class has this

public $belongsTo = array(

        'User' =>

            array(

                'className'              => 'User',

                'joinTable'              => 'users',  

                'foreignKey'             => 'user_id',

                'unique'                 => true),

        'Role' =>

            array(

                'className'              => 'Role',

                'joinTable'              => 'roles',  

                'foreignKey'             => 'role_id',

                'unique'                 => true)


I am now trying to query the user table to determine the students belonging to the logged in user (a teacher or tutor)


public function getStudents(){

$userId = $this->Auth->user('id');

$conditions = array('conditions' => array (array('Role.id =' => 75),  //75 is the role for all students

array ('RoleUser.id = ' => $userId)));  //this is the current logged in user

$result = $this->User->find('all',$conditions);

debug($result);


In the top of my UsersController, I have:

public $uses = array('Role', 'RoleUser');


When I run the function getStudents above, I am getting Error: Call to a member function find() on a non-object


);

--
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 post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: