开发者

Want to save data field from form into two columns of two models

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->Ses开发者_开发问答sion->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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜