CakePHP关联错误?Won't保存关联表


CakePHP Associations error? Won't save associated table

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

但是当我点击保存没有更新?

这是用户模型:模型/User.php

<?php
class User extends AppModel {
    public $name = 'User';
    public $hasMany = array (
    'UserWeb'=>array(
        'className'=>'UserWeb',
        'foreignKey'=>'user_id'
        )
    );
}
?>

UserWeb模型:模型/UserWeb.php

<?php
class UserWeb extends AppModel {
public $name = 'UserWeb';
public $belongsTo = array('User' =>
    array('className'  => 'User',
        'conditions' => '',
    'order'      => '',
    'foreignKey' => 'user_id'
    )
    );
}
?>

用户的控制器:

$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'));
    }
}

和View表单:

<?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');
?>

有人能帮忙吗?我花了很多时间寻找解决方法。当我提交表单时,它不会在user_web表中存储新信息。

顺便说一下,这是表格:

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

您正在尝试关联模型的数据。你的主模型没有数据可以保存。

这里有两种方法,

  1. 你现在的方法是,取User的信息,让他们提供相同形式的UserWeb信息。用户将提供信息作为注册的一部分。为此,您应该参考save Associated model data - saveAll()

  2. 第二种方法,让用户在注册时提供他们的信息,然后让他们添加UsersWeb。(后来我的意思是用户可以在注册期间更新它,但不是以相同的形式)。为此使用UserWeb。有相同的形式出现从/yourapp/UsersWebs/add。您必须提供user_id

p。店员:我强烈建议您再检查一遍"保存数据"。这几乎不会花你10分钟,我相信你以前做过,但在遇到问题后再花10分钟会帮助你理解这些事情。