0

I have the following code in edit method that allows a user to edit their account details. It uses both the User and Profile models and should be updating both.

I have used the contain in the find to load in the Profile information and also called the Profile model using the $uses var at the top of the controller.

But how do I load in both models into the read method?

$user = $this->User->find('first', array( 
                    'conditions' => array('User.id' => $this->Auth->user('id')),
                    'contain'=>'Profile'
                ));

        if ($this->request->is('post') || $this->request->is('put'))
        {
            if ($this->User->save($this->request->data))
            {
                $this->Session->setFlash(__('Your account has been saved'));
                $this->redirect(array('controller'=>'users','action'=>'edit'));
            }
            else
            {
                $this->Session->setFlash(__('Whoops! Something went wrong... try again?'));
            }
        }
        else
        {
            $this->request->data = $this->User->read(null, $user['User']['id']);
        }

3 Answers 3

1

As far as I can tell from the docs read() only brings the expected fields. If you want to read related Models use find instead.

Instead of this:

$this->request->data = $this->User->read(null, $user['User']['id']);

use this:

$this->request->data = $this->User->find('all', 
       array(
         'conditions' => array('User.id' => $user['User']['id']), 
         'contain'    => 'Profile'
       )
);

http://book.cakephp.org/1.3/en/view/1018/find

Sign up to request clarification or add additional context in comments.

Comments

0

You can call the contain() method just before read().

$this->User->contain(array('Profile'));
$this->request->data = $this->User->read(null, $user['User']['id']);

Please note that the effect of this call to contain() will not persist after any call to read() of find().

Comments

0

Just did this: $this->request->data = $user;

And seems to work fine. Can anyone see any issues with doing it this way?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.