CakePHP关联错误?韩元';不保存关联表

CakePHP关联错误?韩元';不保存关联表,php,sql,cakephp,associations,model-associations,Php,Sql,Cakephp,Associations,Model Associations,我创建了一个用户配置文件,用户可以在其中添加信息。所以,在URL部分,我想让他们把他们的其他链接,如Facebook,www.Facebook.com/user等 但是当我点击SaveNothing更新时 以下是适用于用户的模型: Models/User.php <?php class User extends AppModel { public $name = 'User'; public $hasMany = array ( 'UserWeb'=>ar

我创建了一个用户配置文件,用户可以在其中添加信息。所以,在URL部分,我想让他们把他们的其他链接,如Facebook,www.Facebook.com/user等

但是当我点击SaveNothing更新时

以下是适用于用户的模型: Models/User.php

<?php
class User extends AppModel {

    public $name = 'User';

    public $hasMany = array (
    'UserWeb'=>array(
        'className'=>'UserWeb',
        'foreignKey'=>'user_id'
        )
    );
}
?>
及浏览表格:

<?php
    echo $this->Form->create();
echo $this->Form->input('UserWeb.title',
         array('label'=>false,'placeholder'=>'Title'));
echo $this->Form->input('UserWeb.url',
         array('label'=>false,'placeholder'=>'Enter URL'));
echo $this->Form->end('Save');
?>

您正在尝试关联模型的数据。并且您的主模型没有要保存的数据

你可以采取两种方法

  • 以您目前的方式,获取
    用户
    的信息,并让他们从同一表单中提供
    用户网络
    信息。使用将提供信息作为注册的一部分。对此,您应该参考

  • 第二种方法是,让用户在注册时提供他们的信息,然后让他们添加
    UsersWeb
    。(稍后我指的是用户也可以在注册期间更新,但形式不同)。对于此用途
    UserWeb
    。在
    /yourapp/UsersWebs/add
    中显示相同的表单。您必须提供
    用户id
    。这将更加精致

  • 附言:我强烈建议你再检查一遍。这几乎不需要你10分钟,我相信你以前也这样做过,但在遇到问题后再抽出10分钟,将有助于你理解这些事情

    $this->User->id = $this->Auth->user('id');
    if ($this->request->is('post')) {
        if ($this->User->save($this->request->data, array('validate' => false))) {
            $this->Session->setFlash('Profile updated succsessefully!',
                                         'default', array('class' => 'okmsg'));
    
        $this->redirect($this->request->here);
        } else {
          $this->Session->setFlash('Profile could not be saved. Please, try again.',
                                           'default', array('class' => 'errormsg'));
        }
    }
    
    <?php
        echo $this->Form->create();
    echo $this->Form->input('UserWeb.title',
             array('label'=>false,'placeholder'=>'Title'));
    echo $this->Form->input('UserWeb.url',
             array('label'=>false,'placeholder'=>'Enter URL'));
    echo $this->Form->end('Save');
    ?>
    
    CREATE TABLE `user_webs` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `user_id` int(11) DEFAULT '0',
      `title` varchar(128) DEFAULT NULL,
      `url` varchar(128) DEFAULT NULL,
      `created` datetime DEFAULT NULL,
      `modified` datetime DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `web` (`user_id`),
      CONSTRAINT `web` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8