问题描述
我创建了一个用户配置文件,用户可以添加自己的信息。因此,对URL部分,我希望他们把自己的其他环节,如Facebook,www.facebook.com/user等。
但是,当我点击保存什么更新?
下面是用户模型:
型号/ user.php的
< PHP
类用户扩展AppModel { 公共$名='用户'; 公共$ =的hasMany阵列(
UserWeb'=>阵列(
'的className =>'UserWeb',
FOREIGNKEY'=>'USER_ID
)
);
}
?>
有关UserWeb模型:
型号/ UserWeb.php
< PHP
类UserWeb扩展AppModel {公共$ NAME ='UserWeb';公共$ =的belongsTo阵列('用户'=>
阵列('的className =>用户,
'条件'=> '',
秩序= GT; '',
FOREIGNKEY'=> '用户名'
)
);
}
?>
用户控制器:
$这个 - >用户 - > ID = $这个 - > Auth->用户('身份证');
如果($这个 - >请求 - '是('后')){
如果($这个 - >用户 - >保存($这个 - >请求 - >数据阵列('验证'=> FALSE))){
$这个 - >会话级> setFlash('简介succsessefully更新!',
默认,阵列('类'= GT;'okmsg')); $这个 - >重定向($这个 - >请求 - >此处);
}其他{
$这个 - >会话级> setFlash('个人资料无法保存请重试。,
默认,阵列('类'= GT;'ERRORMSG'));
}
}
和视图形式:
< PHP
回声$这个 - >&形式 - GT;创建();
这 - 回声$>&形式 - GT;输入('UserWeb.title',
阵列('标签'=>假的,'占位'=>'标题'));
这 - 回声$>&形式 - GT;输入('UserWeb.url',
阵列('标签'=>假的,'占位'=>输入网址));
回声$这个 - >&形式 - GT;结束(保存);
?>
有人能帮助吗?我试图找了很多时间的解决方案。
当我提交表单也不会新的信息存储在user_webs表。
和BTW这里是表格:
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`日期时间DEFAULT NULL,
`modified`日期时间DEFAULT NULL,
PRIMARY KEY(`id`)
KEY`web`(`user_id`)
约束`web`外键(`user_id`)参考`users`(`id`)
)ENGINE = InnoDB的AUTO_INCREMENT = 8默认字符集= UTF8
您正试图关联的模型数据。和你的主模型没有要保存的数据。
您可以在这里采取两种办法,
-
您present方式,采取
用户
的信息,并让他们提供UserWeb
信息从相同的形式。使用将提供信息作为注册的一部分。为此,您应该参考保存相关模型数据 - 白水回收() -
二的方式,具有用户注册过程中提供了信息,并让他们添加
UsersWeb
之后。 (由后来我的意思是用户可以以同样的形式注册时对其进行更新,但不是)。对于这种使用UserWeb
。具有相同的形式从/ yourapp / UsersWebs /添加出现
。你将不得不提供USER_ID
。这将更加细化。
PS:我强烈建议你去通过的一次。这将很难带你10分钟,我相信你已经这样做过,但已经遇到的问题后,又不放过10分钟将帮助你了解这些事情作为你的手背。
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
You are trying to Associated model's data. And your primary Model does not have data to save.
You can take two approaches here,
Your present way, take
User
's info and have them provideUserWeb
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()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 useUserWeb
. Have the same form appear from/yourapp/UsersWebs/add
. You will have to provide theuser_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.
这篇关于CakePHP的关联错误?将不保存关联表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!