Thursday, June 25, 2009

Re: retrieve profile id from session

This is my code in profiles controller:


    function add($id=null){
        $this->layout = 'add_profile';
       
        $profileCount = $this->User->Profile->find('count', array('conditions'=> array('Profile.user_id' => $this->Auth->user('id') ) ) );
        if ($profileCount !=0) {
                $this->Session->setflash('Profile already exists.');
                $this->redirect('/');
        }
        $user_id = $this->Session->read('Auth.User.id');
        if (!empty($this->data['Profile'])) {
               $this->data['Profile']['class'] = 'User';
               $this->data['Profile']['user_id'] = $user_id;
               $this->User->Profile->create();
                      if ($this->User->Profile->save($this->data)) {
                               $this->Session->setflash('Profile has been saved.');
                               $this->redirect('/');
                       }
                       $this->Session->setflash('Profile cannot be saved.Please, try again.');
           
        }
        $user = $this->User->read(null, $id);
                  $this->set(compact('user'));
                $this->set('profileCount', $profileCount);
                     
    } 

May i change the $user = $this->User->read(null, $id); with something like $user = $this->User->Profile->read(null, $id); ???
Or it is wrong?

2009/6/25 Stu <greenmushroom16@gmail.com>

There are a few ways you could get that,

just for the hell of it, when you say you used:
$user['Profile']['id'], was that in the User's views or the Profile's
views.  "$user" will only be available to your views to which were
passed the variable from the controller $this->set(compact('user'));

hopefully it was just that.  If not, you could set the user info in
either a cookie or session variable, I personally find it a better
approach to set it in a Session variable.  Therefore, you will have to
set it up in your authentication component.  As soon as the user logs
in, do:

$this->Session->write("variable_name", "value");

So you would do something like:

$user_info = $this->User->find('first', array('conditions'=>array
('id'=>$this->data['User']['id'])));
$this->Session->write('user_info', $user_info);

then, to read this from your view...

$this->Session->read('user_info');

Hope this helps,
Good Luck




--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: