Thursday, December 26, 2013

Re: List of all Users belonging to Company?

Set up your paginate just as you would a normal search using the logged in users 'company_id' as the search criteria.

$this->paginate = array(
    'conditions' => array(
        'User.company_id' => $this->Auth->user('company_id')));

$this->set('users', $this->Paginator->paginate());

I believe that should do it if I'm not mistaken. 

On Tuesday, December 24, 2013 1:19:04 PM UTC-5, Shahana Ely wrote:
I have a Model of Users,
and a Model of Companies

each User belongs to a Company:
   public $belongsTo = 'Company';

each Company has many Users:
 public $hasMany = 'User';

A user logs in. It's an ordinary user (not an administrator).

I'm in the UsersController.php, function index, and I want to display only other users that belong to the same company the logged in user belongs to. How do I fetch that info?

e.g.
$this->set('users', $this->Paginator->paginate());

will display all users. Suitable for admin users, not for regular users. How do I restrict the list of users to only users belonging to the same company the user belongs to?

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: