On Sat, Jan 22, 2011 at 3:53 PM, Yaron <[email protected]> wrote:
> Hi,
> Suppose I a users table, a groups table, and every user belongs to a
> group. I'd like via the app_controller to get the group's name, in
> order to set it in the view.
> I've used the following code in app_controller.php:
> function beforeRender(){
>        $this->loadModel('Group');
>        $group = $this->Group->find('id = ' . $this->Auth->user('group_id'));
>        $this->set('groupName', $group['Group']['name']);
> }
>
> This code works great, but is there another way than using the
> loadModel method? thanks.

You can grab the name from within login() and store it in the session.
Assuming you have $this->Auth->autoRedirect = false in
AppController::beforeFilter() ...

public function login()
{
        if ($user = $this->Auth->user())
        {
                $this->User->Group->id = $this->Auth->user('group_id');
                
                $this->Session->write(
                        'Auth.User.group_name',
                        $this->User->Group->field('name')
                );
                
                $this->redirect($this->Auth->redirect());
        }
}

Then you can get the name from within a controller:
$this->Auth->user('group_name');

... or view:
$this->Session->read('Auth.User.group_name');

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to