views:

22

answers:

1

I have a Profile model with a hasOne relationship to a Detail model. I have a registration form that saves data into both model's tables, but I want the username field from the profile model to be copied over to the
usernamefield in the details model so that each has the same username.

function new_account()
{
    if(!empty($this->data))
    {
        $this->Profile->modified = date("Y-m-d H:i:s");                 
        if($this->Profile->save($this->data))
        {
            $this->data['Detail']['profile_id'] = $this->Profile->id;
            $this->data['Detail']['username'] = $this->Profile->username;

        $this->Profile->Detail->save($this->data);
        $this->Session->setFlash('Your registration was successful.');

                    $this->redirect(array('action'=>'index'));
        }
    }

}

This code in my Profile controller gives me the error:

Undefined property: Profile::$username

Any ideas?

+1  A: 

You should be able to simply replace $this->Profile->username with $this->data['Profile']['username'].

You could also store the result of $this->Profile->save($this->data) in a local variable from which you could then extract the username, especially if the username might be altered, for example, in the beforeSave() callback.

The error message you got is normal though. CakePHP does not automatically create properties that correspond to column names.

Mike
Also, `$this->Profile->modified = date("Y-m-d H:i:s");` will not work as you might expect. You will need to assign the return value of `date("Y-m-d H:i:s")` to `$this->data['Profile']['modified']` instead.
Mike