Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 要将表单中的数据字段保存到两个模型的两列中吗_Php_Mysql_Cakephp_Frameworks - Fatal编程技术网

Php 要将表单中的数据字段保存到两个模型的两列中吗

Php 要将表单中的数据字段保存到两个模型的两列中吗,php,mysql,cakephp,frameworks,Php,Mysql,Cakephp,Frameworks,我有一个与细节模型具有hasOne关系的剖面模型。我有一个将数据保存到两个模型表中的注册表,但我希望将配置文件模型中的username字段复制到 详细信息模型中的用户名字段,以便每个字段都具有相同的用户名 function new_account() { if(!empty($this->data)) { $this->Profile->modified = date("Y-m-d H:i:s");

我有一个与细节模型具有hasOne关系的剖面模型。我有一个将数据保存到两个模型表中的注册表,但我希望将配置文件模型中的
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'));
        }
    }

}
“我的配置文件控制器”中的此代码显示错误:

Undefined property: Profile::$username

有什么想法吗?

你应该能够简单地用
$this->Profile->username->data['Profile']['username']
替换
$this->data['Profile']['username']

您还可以将
$this->Profile->save($this->data)
的结果存储在一个局部变量中,然后从中提取用户名,特别是如果用户名可能会被更改,例如在beforeSave()回调中


不过,您收到的错误消息是正常的。CakePHP不会自动创建与列名对应的属性

另外,
$this->Profile->modified=date(“Y-m-dh:i:s”)将无法按预期工作。您需要将
date(“Y-m-dh:i:s”)
的返回值分配给
$this->data['Profile']['modified']