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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题