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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。