douke6027 2013-03-15 00:28
浏览 38
已采纳

CakePHP关联错误? 不会保存关联表

I created a user profile where users can add their information. So on URL section I want them to put their other links like Facebook, www.facebook.com/user etc.

But when I click Save nothing updates?

Here is the Model for User: Models/User.php

<?php
class User extends AppModel {

    public $name = 'User';

    public $hasMany = array (
    'UserWeb'=>array(
        'className'=>'UserWeb',
        'foreignKey'=>'user_id'
        )
    );
}
?>

The model for UserWeb: Models/UserWeb.php

<?php
class UserWeb extends AppModel {

public $name = 'UserWeb';

public $belongsTo = array('User' =>
    array('className'  => 'User',
        'conditions' => '',
    'order'      => '',
    'foreignKey' => 'user_id'
    )
    );
}
?>

The controller for user:

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

And the View form:

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

Can Anyone help please? I'm trying to find solution for a lot of time. When I submit the form it won't store new informations in user_webs table.

And btw here is the table:

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条回答 默认 最新

  • dongpin1850 2013-03-15 02:46
    关注

    You are trying to Associated model's data. And your primary Model does not have data to save.

    You can take two approaches here,

    1. Your present way, take User's info and have them provide UserWeb information from the same form. The use will provide the information as a part of registration. For this you should refer to Saving Associated model data - saveAll()

    2. Second way, have user provide their info during registration and let them add UsersWeb later. (by later I mean user's can update it during registration as well but not in same form). For this use UserWeb. Have the same form appear from /yourapp/UsersWebs/add. You will have to provide the user_id. This will be more refined.

    P.S: I strongly recommend you to go through Saving your data again. It will hardly take you 10 mins, I am sure you have done this before but sparing another 10 mins after having encountered the issue will help you understand these things as back of your hand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)